src/audio/openslES/SDL_openslES.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Jun 2019 15:09:15 -0700
changeset 12766 a85ea89b0561
parent 12754 aec84759c57e
child 12767 8b07df58da32
permissions -rw-r--r--
Fixed surround sound channel setup for Android OpenSL ES audio driver
slouken@12534
     1
/*
slouken@12534
     2
  Simple DirectMedia Layer
slouken@12534
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@12534
     4
slouken@12534
     5
  This software is provided 'as-is', without any express or implied
slouken@12534
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@12534
     7
  arising from the use of this software.
slouken@12534
     8
slouken@12534
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@12534
    10
  including commercial applications, and to alter it and redistribute it
slouken@12534
    11
  freely, subject to the following restrictions:
slouken@12534
    12
slouken@12534
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@12534
    14
     claim that you wrote the original software. If you use this software
slouken@12534
    15
     in a product, an acknowledgment in the product documentation would be
slouken@12534
    16
     appreciated but is not required.
slouken@12534
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@12534
    18
     misrepresented as being the original software.
slouken@12534
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@12534
    20
*/
slouken@12534
    21
#include "../../SDL_internal.h"
slouken@12534
    22
slouken@12534
    23
#if SDL_AUDIO_DRIVER_OPENSLES
slouken@12534
    24
slouken@12534
    25
#include "SDL_audio.h"
slouken@12534
    26
#include "../SDL_audio_c.h"
slouken@12534
    27
#include "SDL_openslES.h"
slouken@12534
    28
sylvain@12540
    29
/* for native audio */
slouken@12534
    30
#include <SLES/OpenSLES.h>
slouken@12534
    31
#include <SLES/OpenSLES_Android.h>
slouken@12534
    32
slouken@12534
    33
#include <android/log.h>
slouken@12534
    34
slouken@12534
    35
#define LOG_TAG "SDL_openslES"
slouken@12534
    36
sylvain@12541
    37
#if 0
slouken@12754
    38
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
sylvain@12541
    39
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
slouken@12754
    40
//#define LOGV(...)  __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
slouken@12754
    41
#define LOGV(...)
sylvain@12541
    42
#else
slouken@12754
    43
#define LOGE(...)
slouken@12534
    44
#define LOGI(...)
slouken@12754
    45
#define LOGV(...)
sylvain@12541
    46
#endif
slouken@12534
    47
sylvain@12540
    48
/* engine interfaces */
slouken@12534
    49
static SLObjectItf engineObject = NULL;
sylvain@12598
    50
static SLEngineItf engineEngine = NULL;
slouken@12534
    51
sylvain@12540
    52
/* output mix interfaces */
slouken@12534
    53
static SLObjectItf outputMixObject = NULL;
sylvain@12540
    54
// static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
slouken@12534
    55
sylvain@12540
    56
/* aux effect on the output mix, used by the buffer queue player */
sylvain@12540
    57
/* static const SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR; */
slouken@12534
    58
sylvain@12540
    59
/* buffer queue player interfaces */
sylvain@12543
    60
static SLObjectItf                   bqPlayerObject      = NULL;
sylvain@12543
    61
static SLPlayItf                     bqPlayerPlay        = NULL;
sylvain@12543
    62
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue = NULL;
sylvain@12598
    63
#if 0
sylvain@12598
    64
static SLEffectSendItf               bqPlayerEffectSend  = NULL;
sylvain@12543
    65
static SLMuteSoloItf                 bqPlayerMuteSolo    = NULL;
sylvain@12543
    66
static SLVolumeItf                   bqPlayerVolume      = NULL;
sylvain@12598
    67
#endif
slouken@12534
    68
sylvain@12538
    69
#if 0
sylvain@12540
    70
/* recorder interfaces TODO */
sylvain@12538
    71
static SLObjectItf                   recorderObject = NULL;
sylvain@12538
    72
static SLRecordItf                   recorderRecord;
sylvain@12538
    73
static SLAndroidSimpleBufferQueueItf recorderBufferQueue;
sylvain@12538
    74
#endif
slouken@12534
    75
sylvain@12540
    76
/* pointer and size of the next player buffer to enqueue, and number of remaining buffers */
sylvain@12538
    77
#if 0
sylvain@12538
    78
static short      *nextBuffer;
sylvain@12538
    79
static unsigned    nextSize;
sylvain@12538
    80
static int         nextCount;
sylvain@12538
    81
#endif
slouken@12534
    82
sylvain@12540
    83
// static SDL_AudioDevice* audioDevice = NULL;
slouken@12534
    84
slouken@12534
    85
#if 0
sylvain@12538
    86
static const char *sldevaudiorecorderstr = "SLES Audio Recorder";
sylvain@12538
    87
static const char *sldevaudioplayerstr   = "SLES Audio Player";
sylvain@12538
    88
sylvain@12538
    89
#define  SLES_DEV_AUDIO_RECORDER  sldevaudiorecorderstr
sylvain@12538
    90
#define  SLES_DEV_AUDIO_PLAYER  sldevaudioplayerstr
slouken@12534
    91
static void openslES_DetectDevices( int iscapture )
slouken@12534
    92
{
sylvain@12538
    93
  LOGI( "openSLES_DetectDevices()" );
slouken@12534
    94
    if ( iscapture )
slouken@12534
    95
            addfn( SLES_DEV_AUDIO_RECORDER );
sylvain@12538
    96
  else
slouken@12534
    97
            addfn( SLES_DEV_AUDIO_PLAYER );
sylvain@12538
    98
  return;
slouken@12534
    99
}
slouken@12534
   100
#endif
slouken@12534
   101
sylvain@12540
   102
static void openslES_DestroyEngine();
slouken@12534
   103
sylvain@12538
   104
static int
sylvain@12540
   105
openslES_CreateEngine()
slouken@12534
   106
{
sylvain@12538
   107
    SLresult result;
slouken@12534
   108
sylvain@12538
   109
    LOGI("openSLES_CreateEngine()");
slouken@12534
   110
sylvain@12540
   111
    /* create engine */
sylvain@12538
   112
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
sylvain@12538
   113
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   114
        LOGE("slCreateEngine failed");
sylvain@12538
   115
        goto error;
sylvain@12538
   116
    }
slouken@12534
   117
sylvain@12538
   118
    LOGI("slCreateEngine OK");
slouken@12534
   119
sylvain@12540
   120
    /* realize the engine */
sylvain@12538
   121
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
sylvain@12538
   122
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   123
        LOGE("RealizeEngine failed");
sylvain@12538
   124
        goto error;
sylvain@12538
   125
    }
slouken@12534
   126
sylvain@12538
   127
    LOGI("RealizeEngine OK");
slouken@12534
   128
sylvain@12540
   129
    /* get the engine interface, which is needed in order to create other objects */
sylvain@12538
   130
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
sylvain@12538
   131
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   132
        LOGE("EngineGetInterface failed");
sylvain@12538
   133
        goto error;
sylvain@12538
   134
    }
slouken@12534
   135
sylvain@12538
   136
    LOGI("EngineGetInterface OK");
slouken@12534
   137
sylvain@12540
   138
    /* create output mix, with environmental reverb specified as a non-required interface */
sylvain@12540
   139
    /* const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB }; */
sylvain@12540
   140
    /* const SLboolean req[1] = { SL_BOOLEAN_FALSE }; */
slouken@12534
   141
sylvain@12538
   142
    const SLInterfaceID ids[1] = { SL_IID_VOLUME };
sylvain@12538
   143
    const SLboolean req[1] = { SL_BOOLEAN_FALSE };
sylvain@12538
   144
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
slouken@12534
   145
sylvain@12538
   146
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   147
        LOGE("CreateOutputMix failed");
sylvain@12538
   148
        goto error;
sylvain@12538
   149
    }
sylvain@12538
   150
    LOGI("CreateOutputMix OK");
slouken@12534
   151
sylvain@12540
   152
    /* realize the output mix */
sylvain@12538
   153
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
sylvain@12538
   154
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   155
        LOGE("RealizeOutputMix failed");
sylvain@12538
   156
        goto error;
sylvain@12538
   157
    }
sylvain@12538
   158
    return 1;
slouken@12534
   159
sylvain@12538
   160
error:
sylvain@12538
   161
    openslES_DestroyEngine();
sylvain@12538
   162
    return 0;
slouken@12534
   163
}
slouken@12534
   164
sylvain@12540
   165
static void openslES_DestroyPCMPlayer(_THIS);
sylvain@12540
   166
static void openslES_DestroyPCMRecorder(_THIS);
slouken@12534
   167
sylvain@12540
   168
static void openslES_DestroyEngine()
slouken@12534
   169
{
sylvain@12538
   170
    LOGI("openslES_DestroyEngine()");
sylvain@12541
   171
sylvain@12540
   172
//        openslES_DestroyPCMPlayer(this);
sylvain@12540
   173
//    openslES_DestroyPCMRecorder(this);
slouken@12534
   174
sylvain@12540
   175
    /* destroy output mix object, and invalidate all associated interfaces */
sylvain@12538
   176
    if (outputMixObject != NULL) {
sylvain@12538
   177
        (*outputMixObject)->Destroy(outputMixObject);
sylvain@12538
   178
        outputMixObject = NULL;
sylvain@12540
   179
        /* outputMixEnvironmentalReverb = NULL; */
sylvain@12538
   180
    }
slouken@12534
   181
sylvain@12540
   182
    /* destroy engine object, and invalidate all associated interfaces */
sylvain@12538
   183
    if (engineObject != NULL) {
sylvain@12538
   184
        (*engineObject)->Destroy(engineObject);
sylvain@12538
   185
        engineObject = NULL;
sylvain@12538
   186
        engineEngine = NULL;
sylvain@12538
   187
    }
slouken@12534
   188
sylvain@12538
   189
    return;
slouken@12534
   190
}
slouken@12534
   191
sylvain@12540
   192
/* this callback handler is called every time a buffer finishes playing */
sylvain@12538
   193
static void
sylvain@12538
   194
bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
slouken@12534
   195
{
sylvain@12540
   196
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) context;
slouken@12754
   197
    LOGV("SLES: Playback Callmeback");
sylvain@12540
   198
    SDL_SemPost(audiodata->playsem);
sylvain@12538
   199
    return;
slouken@12534
   200
}
slouken@12534
   201
sylvain@12538
   202
static int
sylvain@12538
   203
openslES_CreatePCMRecorder(_THIS)
slouken@12534
   204
{
sylvain@12540
   205
/*    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   206
sylvain@12538
   207
    LOGE("openslES_CreatePCMRecorder not implimented yet!");
sylvain@12538
   208
    return SDL_SetError("openslES_CreatePCMRecorder not implimented yet!");
slouken@12534
   209
}
slouken@12534
   210
sylvain@12538
   211
static void
sylvain@12540
   212
openslES_DestroyPCMRecorder(_THIS)
slouken@12534
   213
{
sylvain@12540
   214
/*    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   215
sylvain@12538
   216
    return;
slouken@12534
   217
}
slouken@12534
   218
sylvain@12538
   219
static int
sylvain@12539
   220
openslES_CreatePCMPlayer(_THIS)
slouken@12534
   221
{
sylvain@12540
   222
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12538
   223
    SLDataFormat_PCM format_pcm;
sylvain@12538
   224
    SLresult result;
sylvain@12538
   225
    int i;
slouken@12534
   226
slouken@12754
   227
    /* If we want to add floating point audio support (requires API level 21)
slouken@12754
   228
       it can be done as described here:
slouken@12754
   229
        https://developer.android.com/ndk/guides/audio/opensl/android-extensions.html#floating-point
slouken@12754
   230
    */
slouken@12754
   231
#if 1
slouken@12754
   232
    /* Just go with signed 16-bit audio as it's the most compatible */
slouken@12754
   233
    this->spec.format = AUDIO_S16SYS;
slouken@12754
   234
#else
slouken@12754
   235
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@12754
   236
    while (test_format != 0) {
slouken@12754
   237
        if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
slouken@12754
   238
            break;
slouken@12754
   239
        }
slouken@12754
   240
        test_format = SDL_NextAudioFormat();
slouken@12754
   241
    }
slouken@12534
   242
slouken@12754
   243
    if (test_format == 0) {
slouken@12754
   244
        /* Didn't find a compatible format : */
slouken@12754
   245
        LOGI( "No compatible audio format, using signed 16-bit audio" );
slouken@12754
   246
        test_format = AUDIO_S16SYS;
slouken@12754
   247
    }
slouken@12754
   248
    this->spec.format = test_format;
sylvain@12540
   249
#endif
slouken@12534
   250
sylvain@12540
   251
    /* Update the fragment size as size in bytes */
sylvain@12538
   252
    SDL_CalculateAudioSpec(&this->spec);
sylvain@12538
   253
sylvain@12538
   254
    LOGI("Try to open %u hz %u bit chan %u %s samples %u",
sylvain@12538
   255
          this->spec.freq, SDL_AUDIO_BITSIZE(this->spec.format),
slouken@12754
   256
          this->spec.channels, (this->spec.format & 0x1000) ? "BE" : "LE", this->spec.samples);
slouken@12534
   257
sylvain@12540
   258
    /* configure audio source */
sylvain@12597
   259
    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS };
slouken@12534
   260
sylvain@12538
   261
    format_pcm.formatType    = SL_DATAFORMAT_PCM;
sylvain@12538
   262
    format_pcm.numChannels   = this->spec.channels;
sylvain@12540
   263
    format_pcm.samplesPerSec = this->spec.freq * 1000;  /* / kilo Hz to milli Hz */
sylvain@12538
   264
    format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
sylvain@12538
   265
    format_pcm.containerSize = SDL_AUDIO_BITSIZE(this->spec.format);
slouken@12534
   266
sylvain@12538
   267
    if (SDL_AUDIO_ISBIGENDIAN(this->spec.format)) {
sylvain@12538
   268
        format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
sylvain@12538
   269
    } else {
sylvain@12538
   270
        format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
sylvain@12538
   271
    }
slouken@12534
   272
slouken@12534
   273
/*
sylvain@12538
   274
#define SL_SPEAKER_FRONT_LEFT            ((SLuint32) 0x00000001)
sylvain@12538
   275
#define SL_SPEAKER_FRONT_RIGHT           ((SLuint32) 0x00000002)
sylvain@12538
   276
#define SL_SPEAKER_FRONT_CENTER          ((SLuint32) 0x00000004)
sylvain@12538
   277
#define SL_SPEAKER_LOW_FREQUENCY         ((SLuint32) 0x00000008)
sylvain@12538
   278
#define SL_SPEAKER_BACK_LEFT             ((SLuint32) 0x00000010)
sylvain@12538
   279
#define SL_SPEAKER_BACK_RIGHT            ((SLuint32) 0x00000020)
sylvain@12538
   280
#define SL_SPEAKER_FRONT_LEFT_OF_CENTER  ((SLuint32) 0x00000040)
sylvain@12538
   281
#define SL_SPEAKER_FRONT_RIGHT_OF_CENTER ((SLuint32) 0x00000080)
sylvain@12538
   282
#define SL_SPEAKER_BACK_CENTER           ((SLuint32) 0x00000100)
sylvain@12538
   283
#define SL_SPEAKER_SIDE_LEFT             ((SLuint32) 0x00000200)
sylvain@12538
   284
#define SL_SPEAKER_SIDE_RIGHT            ((SLuint32) 0x00000400)
sylvain@12538
   285
#define SL_SPEAKER_TOP_CENTER            ((SLuint32) 0x00000800)
sylvain@12538
   286
#define SL_SPEAKER_TOP_FRONT_LEFT        ((SLuint32) 0x00001000)
sylvain@12538
   287
#define SL_SPEAKER_TOP_FRONT_CENTER      ((SLuint32) 0x00002000)
sylvain@12538
   288
#define SL_SPEAKER_TOP_FRONT_RIGHT       ((SLuint32) 0x00004000)
sylvain@12538
   289
#define SL_SPEAKER_TOP_BACK_LEFT         ((SLuint32) 0x00008000)
sylvain@12538
   290
#define SL_SPEAKER_TOP_BACK_CENTER       ((SLuint32) 0x00010000)
sylvain@12538
   291
#define SL_SPEAKER_TOP_BACK_RIGHT        ((SLuint32) 0x00020000)
slouken@12534
   292
*/
slouken@12766
   293
#define SL_ANDROID_SPEAKER_QUAD (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT)
slouken@12766
   294
#define SL_ANDROID_SPEAKER_5DOT1 (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER  | SL_SPEAKER_LOW_FREQUENCY| SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT)
slouken@12766
   295
#define SL_ANDROID_SPEAKER_7DOT1 (SL_ANDROID_SPEAKER_5DOT1 | SL_SPEAKER_SIDE_LEFT | SL_SPEAKER_SIDE_RIGHT)
slouken@12534
   296
slouken@12766
   297
    switch (this->spec.channels)
slouken@12766
   298
    {
slouken@12766
   299
    case 1:
slouken@12766
   300
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
slouken@12766
   301
        break;
slouken@12766
   302
    case 2:
sylvain@12538
   303
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
slouken@12766
   304
        break;
slouken@12766
   305
    case 3:
sylvain@12538
   306
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER;
slouken@12766
   307
        break;
slouken@12766
   308
    case 4:
slouken@12766
   309
        format_pcm.channelMask = SL_ANDROID_SPEAKER_QUAD;
slouken@12766
   310
        break;
slouken@12766
   311
    case 5:
slouken@12766
   312
        format_pcm.channelMask = SL_ANDROID_SPEAKER_QUAD | SL_SPEAKER_FRONT_CENTER;
slouken@12766
   313
        break;
slouken@12766
   314
    case 6:
slouken@12766
   315
        format_pcm.channelMask = SL_ANDROID_SPEAKER_5DOT1;
slouken@12766
   316
        break;
slouken@12766
   317
    case 7:
slouken@12766
   318
        format_pcm.channelMask = SL_ANDROID_SPEAKER_5DOT1 | SL_SPEAKER_BACK_CENTER;
slouken@12766
   319
        break;
slouken@12766
   320
    case 8:
slouken@12766
   321
        format_pcm.channelMask = SL_ANDROID_SPEAKER_7DOT1;
slouken@12766
   322
        break;
slouken@12766
   323
    default:
slouken@12766
   324
        /* Unknown number of channels, fall back to stereo */
slouken@12766
   325
        this->spec.channels = 2;
slouken@12766
   326
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
slouken@12766
   327
        break;
sylvain@12538
   328
    }
slouken@12534
   329
sylvain@12538
   330
    SLDataSource audioSrc = { &loc_bufq, &format_pcm };
slouken@12534
   331
sylvain@12540
   332
    /* configure audio sink */
sylvain@12538
   333
    SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
sylvain@12538
   334
    SLDataSink audioSnk = { &loc_outmix, NULL };
slouken@12534
   335
sylvain@12540
   336
    /* create audio player */
sylvain@12538
   337
    const SLInterfaceID ids[2] = {
sylvain@12538
   338
        SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
sylvain@12538
   339
        SL_IID_VOLUME
sylvain@12538
   340
    };
slouken@12534
   341
sylvain@12538
   342
    const SLboolean req[2] = {
sylvain@12538
   343
        SL_BOOLEAN_TRUE,
sylvain@12538
   344
        SL_BOOLEAN_FALSE,
sylvain@12538
   345
    };
slouken@12534
   346
sylvain@12538
   347
    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 2, ids, req);
sylvain@12538
   348
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   349
        LOGE("CreateAudioPlayer failed");
sylvain@12538
   350
        goto failed;
sylvain@12538
   351
    }
slouken@12534
   352
sylvain@12540
   353
    /* realize the player */
sylvain@12538
   354
    result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
sylvain@12538
   355
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   356
        LOGE("RealizeAudioPlayer failed");
sylvain@12538
   357
        goto failed;
sylvain@12538
   358
    }
slouken@12534
   359
sylvain@12540
   360
    /* get the play interface */
sylvain@12543
   361
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
sylvain@12538
   362
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   363
        LOGE("SL_IID_PLAY interface get failed");
sylvain@12538
   364
        goto failed;
sylvain@12538
   365
    }
slouken@12534
   366
sylvain@12540
   367
    /* get the buffer queue interface */
sylvain@12538
   368
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bqPlayerBufferQueue);
sylvain@12538
   369
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   370
        LOGE("SL_IID_BUFFERQUEUE interface get failed");
sylvain@12538
   371
        goto failed;
sylvain@12538
   372
    }
slouken@12534
   373
sylvain@12540
   374
    /* register callback on the buffer queue */
sylvain@12540
   375
    /* context is '(SDL_PrivateAudioData *)this->hidden' */
sylvain@12540
   376
    result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this->hidden);
sylvain@12538
   377
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   378
        LOGE("RegisterCallback failed");
sylvain@12538
   379
        goto failed;
sylvain@12538
   380
    }
slouken@12534
   381
slouken@12534
   382
#if 0
sylvain@12540
   383
    /* get the effect send interface */
sylvain@12538
   384
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend);
sylvain@12538
   385
    if (SL_RESULT_SUCCESS != result)
sylvain@12538
   386
    {
slouken@12534
   387
sylvain@12538
   388
        LOGE("SL_IID_EFFECTSEND interface get failed");
sylvain@12538
   389
        goto failed;
sylvain@12538
   390
    }
slouken@12534
   391
#endif
slouken@12534
   392
sylvain@12540
   393
#if 0   /* mute/solo is not supported for sources that are known to be mono, as this is */
sylvain@12540
   394
    /* get the mute/solo interface */
slouken@12534
   395
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_MUTESOLO, &bqPlayerMuteSolo);
slouken@12534
   396
    assert(SL_RESULT_SUCCESS == result);
sylvain@12538
   397
    (void) result;
slouken@12534
   398
#endif
slouken@12534
   399
sylvain@12598
   400
#if 0
sylvain@12540
   401
    /* get the volume interface */
sylvain@12538
   402
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
sylvain@12538
   403
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   404
        LOGE("SL_IID_VOLUME interface get failed");
sylvain@12540
   405
        /* goto failed; */
sylvain@12538
   406
    }
sylvain@12598
   407
#endif
slouken@12534
   408
slouken@12534
   409
    /* Create the audio buffer semaphore */
sylvain@12540
   410
    audiodata->playsem = SDL_CreateSemaphore(NUM_BUFFERS - 1);
sylvain@12540
   411
    if (!audiodata->playsem) {
sylvain@12538
   412
        LOGE("cannot create Semaphore!");
sylvain@12538
   413
        goto failed;
sylvain@12538
   414
    }
slouken@12534
   415
sylvain@12538
   416
    /* Create the sound buffers */
sylvain@12540
   417
    audiodata->mixbuff = (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
sylvain@12540
   418
    if (audiodata->mixbuff == NULL) {
sylvain@12538
   419
        LOGE("mixbuffer allocate - out of memory");
sylvain@12538
   420
        goto failed;
sylvain@12538
   421
    }
slouken@12534
   422
sylvain@12538
   423
    for (i = 0; i < NUM_BUFFERS; i++) {
sylvain@12540
   424
        audiodata->pmixbuff[i] = audiodata->mixbuff + i * this->spec.size;
sylvain@12538
   425
    }
slouken@12534
   426
sylvain@12542
   427
    /* set the player's state to playing */
sylvain@12543
   428
    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
sylvain@12542
   429
    if (SL_RESULT_SUCCESS != result) {
sylvain@12542
   430
        LOGE("Play set state failed");
sylvain@12542
   431
        goto failed;
sylvain@12542
   432
    }
sylvain@12542
   433
slouken@12534
   434
    return 0;
slouken@12534
   435
sylvain@12538
   436
failed:
slouken@12534
   437
sylvain@12540
   438
    openslES_DestroyPCMPlayer(this);
slouken@12534
   439
sylvain@12538
   440
    return SDL_SetError("Open device failed!");
slouken@12534
   441
}
slouken@12534
   442
sylvain@12538
   443
static void
sylvain@12540
   444
openslES_DestroyPCMPlayer(_THIS)
slouken@12534
   445
{
sylvain@12540
   446
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12541
   447
    SLresult result;
sylvain@12541
   448
sylvain@12541
   449
    /* set the player's state to 'stopped' */
sylvain@12543
   450
    if (bqPlayerPlay != NULL) {
sylvain@12543
   451
        result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED);
sylvain@12543
   452
        if (SL_RESULT_SUCCESS != result) {
sylvain@12543
   453
            SDL_SetError("Stopped set state failed");
sylvain@12543
   454
        }
sylvain@12541
   455
    }
sylvain@12540
   456
sylvain@12540
   457
    /* destroy buffer queue audio player object, and invalidate all associated interfaces */
sylvain@12538
   458
    if (bqPlayerObject != NULL) {
slouken@12534
   459
sylvain@12538
   460
        (*bqPlayerObject)->Destroy(bqPlayerObject);
sylvain@12538
   461
sylvain@12538
   462
        bqPlayerObject = NULL;
sylvain@12543
   463
        bqPlayerPlay = NULL;
slouken@12534
   464
        bqPlayerBufferQueue = NULL;
sylvain@12598
   465
#if 0
sylvain@12598
   466
        bqPlayerEffectSend = NULL;
sylvain@12538
   467
        bqPlayerMuteSolo = NULL;
sylvain@12538
   468
        bqPlayerVolume = NULL;
sylvain@12598
   469
#endif
slouken@12534
   470
    }
slouken@12534
   471
sylvain@12540
   472
    if (audiodata->playsem) {
sylvain@12540
   473
        SDL_DestroySemaphore(audiodata->playsem);
sylvain@12540
   474
        audiodata->playsem = NULL;
sylvain@12538
   475
    }
slouken@12534
   476
sylvain@12540
   477
    if (audiodata->mixbuff) {
sylvain@12540
   478
        SDL_free(audiodata->mixbuff);
sylvain@12538
   479
    }
slouken@12534
   480
sylvain@12538
   481
    return;
slouken@12534
   482
}
slouken@12534
   483
sylvain@12538
   484
static int
sylvain@12538
   485
openslES_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
slouken@12534
   486
{
sylvain@12539
   487
    this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
sylvain@12539
   488
    if (this->hidden == NULL) {
sylvain@12539
   489
        return SDL_OutOfMemory();
sylvain@12539
   490
    }
sylvain@12539
   491
sylvain@12538
   492
    if (iscapture) {
sylvain@12538
   493
        LOGI("openslES_OpenDevice( ) %s for capture", devname);
sylvain@12538
   494
        return openslES_CreatePCMRecorder(this);
sylvain@12538
   495
    } else {
sylvain@12538
   496
        LOGI("openslES_OpenDevice( ) %s for playing", devname);
sylvain@12538
   497
        return openslES_CreatePCMPlayer(this);
sylvain@12538
   498
    }
slouken@12534
   499
}
slouken@12534
   500
sylvain@12538
   501
static void
sylvain@12538
   502
openslES_CloseDevice(_THIS)
slouken@12534
   503
{
sylvain@12540
   504
    /* struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   505
sylvain@12538
   506
    if (this->iscapture) {
sylvain@12538
   507
        LOGI("openslES_CloseDevice( ) for capture");
sylvain@12540
   508
        openslES_DestroyPCMRecorder(this);
sylvain@12538
   509
    } else {
sylvain@12538
   510
        LOGI("openslES_CloseDevice( ) for playing");
sylvain@12540
   511
        openslES_DestroyPCMPlayer(this);
sylvain@12538
   512
    }
sylvain@12541
   513
sylvain@12539
   514
    SDL_free(this->hidden);
sylvain@12539
   515
sylvain@12538
   516
    return;
slouken@12534
   517
}
slouken@12534
   518
sylvain@12538
   519
static void
sylvain@12538
   520
openslES_WaitDevice(_THIS)
slouken@12534
   521
{
sylvain@12540
   522
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   523
slouken@12754
   524
    LOGV("openslES_WaitDevice( )");
slouken@12534
   525
slouken@12534
   526
    /* Wait for an audio chunk to finish */
sylvain@12540
   527
    /* WaitForSingleObject(this->hidden->audio_sem, INFINITE); */
sylvain@12540
   528
    SDL_SemWait(audiodata->playsem);
slouken@12534
   529
sylvain@12538
   530
    return;
slouken@12534
   531
}
slouken@12534
   532
sylvain@12540
   533
/*/           n   playn sem */
sylvain@12540
   534
/* getbuf     0   -     1 */
sylvain@12540
   535
/* fill buff  0   -     1 */
sylvain@12540
   536
/* play       0 - 0     1 */
sylvain@12540
   537
/* wait       1   0     0 */
sylvain@12540
   538
/* getbuf     1   0     0 */
sylvain@12540
   539
/* fill buff  1   0     0 */
sylvain@12540
   540
/* play       0   0     0 */
sylvain@12540
   541
/* wait */
sylvain@12540
   542
/* */
sylvain@12540
   543
/* okay.. */
slouken@12534
   544
sylvain@12538
   545
static Uint8 *
sylvain@12538
   546
openslES_GetDeviceBuf(_THIS)
slouken@12534
   547
{
sylvain@12540
   548
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   549
slouken@12754
   550
    LOGV("openslES_GetDeviceBuf( )");
sylvain@12540
   551
    return audiodata->pmixbuff[audiodata->next_buffer];
slouken@12534
   552
}
slouken@12534
   553
sylvain@12538
   554
static void
sylvain@12538
   555
openslES_PlayDevice(_THIS)
slouken@12534
   556
{
sylvain@12540
   557
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12538
   558
    SLresult result;
slouken@12534
   559
slouken@12754
   560
    LOGV("======openslES_PlayDevice( )======");
sylvain@12596
   561
slouken@12534
   562
    /* Queue it up */
sylvain@12540
   563
    result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
slouken@12534
   564
sylvain@12540
   565
    audiodata->next_buffer++;
sylvain@12540
   566
    if (audiodata->next_buffer >= NUM_BUFFERS) {
sylvain@12540
   567
        audiodata->next_buffer = 0;
sylvain@12538
   568
    }
slouken@12534
   569
sylvain@12596
   570
    /* If Enqueue fails, callback won't be called.
sylvain@12596
   571
     * Post the semphore, not to run out of buffer */
sylvain@12596
   572
    if (SL_RESULT_SUCCESS != result) {
sylvain@12596
   573
        SDL_SemPost(audiodata->playsem);
sylvain@12596
   574
    }
sylvain@12596
   575
sylvain@12538
   576
    return;
slouken@12534
   577
}
slouken@12534
   578
sylvain@12538
   579
static int
sylvain@12538
   580
openslES_Init(SDL_AudioDriverImpl * impl)
slouken@12534
   581
{
sylvain@12538
   582
    LOGI("openslES_Init() called");
slouken@12534
   583
sylvain@12538
   584
    if (!openslES_CreateEngine()) {
sylvain@12538
   585
        return 0;
sylvain@12538
   586
    }
slouken@12534
   587
sylvain@12538
   588
    LOGI("openslES_Init() - set pointers");
slouken@12534
   589
sylvain@12538
   590
    /* Set the function pointers */
sylvain@12540
   591
    /* impl->DetectDevices = openslES_DetectDevices; */
sylvain@12538
   592
    impl->OpenDevice    = openslES_OpenDevice;
sylvain@12539
   593
    impl->CloseDevice   = openslES_CloseDevice;
sylvain@12538
   594
    impl->PlayDevice    = openslES_PlayDevice;
sylvain@12538
   595
    impl->GetDeviceBuf  = openslES_GetDeviceBuf;
sylvain@12538
   596
    impl->Deinitialize  = openslES_DestroyEngine;
sylvain@12538
   597
    impl->WaitDevice    = openslES_WaitDevice;
slouken@12534
   598
sylvain@12538
   599
    /* and the capabilities */
sylvain@12538
   600
    impl->HasCaptureSupport             = 0;        /* TODO */
sylvain@12538
   601
    impl->OnlyHasDefaultOutputDevice    = 1;
sylvain@12540
   602
    /* impl->OnlyHasDefaultInputDevice  = 1; */
slouken@12534
   603
sylvain@12538
   604
    LOGI("openslES_Init() - succes");
slouken@12534
   605
sylvain@12538
   606
    /* this audio target is available. */
sylvain@12538
   607
    return 1;
slouken@12534
   608
}
slouken@12534
   609
slouken@12534
   610
AudioBootStrap openslES_bootstrap = {
sylvain@12538
   611
    "openslES", "opensl ES audio driver", openslES_Init, 0
slouken@12534
   612
};
slouken@12534
   613
sylvain@12541
   614
void openslES_ResumeDevices()
sylvain@12541
   615
{
sylvain@12545
   616
    if (bqPlayerPlay != NULL) {
sylvain@12545
   617
        /* set the player's state to 'playing' */
sylvain@12545
   618
        SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
sylvain@12545
   619
        if (SL_RESULT_SUCCESS != result) {
sylvain@12545
   620
            SDL_SetError("openslES_ResumeDevices failed");
sylvain@12545
   621
        }
sylvain@12541
   622
    }
sylvain@12541
   623
}
sylvain@12541
   624
sylvain@12541
   625
void openslES_PauseDevices()
sylvain@12541
   626
{
sylvain@12545
   627
    if (bqPlayerPlay != NULL) {
sylvain@12545
   628
        /* set the player's state to 'paused' */
sylvain@12545
   629
        SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PAUSED);
sylvain@12545
   630
        if (SL_RESULT_SUCCESS != result) {
sylvain@12545
   631
            SDL_SetError("openslES_PauseDevices failed");
sylvain@12545
   632
        }
sylvain@12541
   633
    }
sylvain@12541
   634
}
sylvain@12541
   635
slouken@12534
   636
#endif /* SDL_AUDIO_DRIVER_OPENSLES */
slouken@12534
   637
slouken@12534
   638
/* vi: set ts=4 sw=4 expandtab: */