src/audio/openslES/SDL_openslES.c
author Sylvain Becker <sylvain.becker@gmail.com>
Mon, 14 Jan 2019 10:58:57 +0100
changeset 12540 2660c01ff7da
parent 12539 e7282e21da92
child 12541 f0b89ca0ad7d
permissions -rw-r--r--
Android/openslES: move a few static variables to SDL_PrivateAudioData structure
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@12540
    37
/*#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
sylvain@12540
    38
/*#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
sylvain@12540
    39
/*#define LOGI(...) do {} while (0) */
sylvain@12540
    40
/*#define LOGE(...) do {} while (0) */
slouken@12534
    41
#define LOGI(...)
slouken@12534
    42
#define LOGE(...)
slouken@12534
    43
sylvain@12540
    44
/* engine interfaces */
slouken@12534
    45
static SLObjectItf engineObject = NULL;
slouken@12534
    46
static SLEngineItf engineEngine;
slouken@12534
    47
sylvain@12540
    48
/* output mix interfaces */
slouken@12534
    49
static SLObjectItf outputMixObject = NULL;
sylvain@12540
    50
// static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
slouken@12534
    51
sylvain@12540
    52
/* aux effect on the output mix, used by the buffer queue player */
sylvain@12540
    53
/* static const SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR; */
slouken@12534
    54
sylvain@12540
    55
/* buffer queue player interfaces */
sylvain@12538
    56
static SLObjectItf                   bqPlayerObject = NULL;
sylvain@12538
    57
static SLPlayItf                     bqPlayerPlay;
sylvain@12538
    58
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
sylvain@12540
    59
/*static SLEffectSendItf          bqPlayerEffectSend; */
sylvain@12538
    60
static SLMuteSoloItf                 bqPlayerMuteSolo;
sylvain@12538
    61
static SLVolumeItf                   bqPlayerVolume;
slouken@12534
    62
sylvain@12538
    63
#if 0
sylvain@12540
    64
/* recorder interfaces TODO */
sylvain@12538
    65
static SLObjectItf                   recorderObject = NULL;
sylvain@12538
    66
static SLRecordItf                   recorderRecord;
sylvain@12538
    67
static SLAndroidSimpleBufferQueueItf recorderBufferQueue;
sylvain@12538
    68
#endif
slouken@12534
    69
sylvain@12540
    70
/* pointer and size of the next player buffer to enqueue, and number of remaining buffers */
sylvain@12538
    71
#if 0
sylvain@12538
    72
static short      *nextBuffer;
sylvain@12538
    73
static unsigned    nextSize;
sylvain@12538
    74
static int         nextCount;
sylvain@12538
    75
#endif
slouken@12534
    76
sylvain@12540
    77
// static SDL_AudioDevice* audioDevice = NULL;
slouken@12534
    78
slouken@12534
    79
#if 0
sylvain@12538
    80
static const char *sldevaudiorecorderstr = "SLES Audio Recorder";
sylvain@12538
    81
static const char *sldevaudioplayerstr   = "SLES Audio Player";
sylvain@12538
    82
sylvain@12538
    83
#define  SLES_DEV_AUDIO_RECORDER  sldevaudiorecorderstr
sylvain@12538
    84
#define  SLES_DEV_AUDIO_PLAYER  sldevaudioplayerstr
slouken@12534
    85
static void openslES_DetectDevices( int iscapture )
slouken@12534
    86
{
sylvain@12538
    87
  LOGI( "openSLES_DetectDevices()" );
slouken@12534
    88
    if ( iscapture )
slouken@12534
    89
            addfn( SLES_DEV_AUDIO_RECORDER );
sylvain@12538
    90
  else
slouken@12534
    91
            addfn( SLES_DEV_AUDIO_PLAYER );
sylvain@12538
    92
  return;
slouken@12534
    93
}
slouken@12534
    94
#endif
slouken@12534
    95
sylvain@12540
    96
static void openslES_DestroyEngine();
slouken@12534
    97
sylvain@12538
    98
static int
sylvain@12540
    99
openslES_CreateEngine()
slouken@12534
   100
{
sylvain@12538
   101
    SLresult result;
slouken@12534
   102
sylvain@12538
   103
    LOGI("openSLES_CreateEngine()");
slouken@12534
   104
sylvain@12540
   105
    /* create engine */
sylvain@12538
   106
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
sylvain@12538
   107
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   108
        LOGE("slCreateEngine failed");
sylvain@12538
   109
        goto error;
sylvain@12538
   110
    }
slouken@12534
   111
sylvain@12538
   112
    LOGI("slCreateEngine OK");
slouken@12534
   113
sylvain@12540
   114
    /* realize the engine */
sylvain@12538
   115
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
sylvain@12538
   116
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   117
        LOGE("RealizeEngine failed");
sylvain@12538
   118
        goto error;
sylvain@12538
   119
    }
slouken@12534
   120
sylvain@12538
   121
    LOGI("RealizeEngine OK");
slouken@12534
   122
sylvain@12540
   123
    /* get the engine interface, which is needed in order to create other objects */
sylvain@12538
   124
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
sylvain@12538
   125
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   126
        LOGE("EngineGetInterface failed");
sylvain@12538
   127
        goto error;
sylvain@12538
   128
    }
slouken@12534
   129
sylvain@12538
   130
    LOGI("EngineGetInterface OK");
slouken@12534
   131
sylvain@12540
   132
    /* create output mix, with environmental reverb specified as a non-required interface */
sylvain@12540
   133
    /* const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB }; */
sylvain@12540
   134
    /* const SLboolean req[1] = { SL_BOOLEAN_FALSE }; */
slouken@12534
   135
sylvain@12538
   136
    const SLInterfaceID ids[1] = { SL_IID_VOLUME };
sylvain@12538
   137
    const SLboolean req[1] = { SL_BOOLEAN_FALSE };
sylvain@12538
   138
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
slouken@12534
   139
sylvain@12538
   140
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   141
        LOGE("CreateOutputMix failed");
sylvain@12538
   142
        goto error;
sylvain@12538
   143
    }
sylvain@12538
   144
    LOGI("CreateOutputMix OK");
slouken@12534
   145
sylvain@12540
   146
    /* realize the output mix */
sylvain@12538
   147
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
sylvain@12538
   148
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   149
        LOGE("RealizeOutputMix failed");
sylvain@12538
   150
        goto error;
sylvain@12538
   151
    }
sylvain@12538
   152
    return 1;
slouken@12534
   153
sylvain@12538
   154
error:
sylvain@12538
   155
    openslES_DestroyEngine();
sylvain@12538
   156
    return 0;
slouken@12534
   157
}
slouken@12534
   158
sylvain@12540
   159
static void openslES_DestroyPCMPlayer(_THIS);
sylvain@12540
   160
static void openslES_DestroyPCMRecorder(_THIS);
slouken@12534
   161
sylvain@12540
   162
static void openslES_DestroyEngine()
slouken@12534
   163
{
sylvain@12538
   164
    LOGI("openslES_DestroyEngine()");
sylvain@12540
   165
   
sylvain@12540
   166
//        openslES_DestroyPCMPlayer(this);
sylvain@12540
   167
//    openslES_DestroyPCMRecorder(this);
slouken@12534
   168
sylvain@12540
   169
    /* destroy output mix object, and invalidate all associated interfaces */
sylvain@12538
   170
    if (outputMixObject != NULL) {
sylvain@12538
   171
        (*outputMixObject)->Destroy(outputMixObject);
sylvain@12538
   172
        outputMixObject = NULL;
sylvain@12540
   173
        /* outputMixEnvironmentalReverb = NULL; */
sylvain@12538
   174
    }
slouken@12534
   175
sylvain@12540
   176
    /* destroy engine object, and invalidate all associated interfaces */
sylvain@12538
   177
    if (engineObject != NULL) {
sylvain@12538
   178
        (*engineObject)->Destroy(engineObject);
sylvain@12538
   179
        engineObject = NULL;
sylvain@12538
   180
        engineEngine = NULL;
sylvain@12538
   181
    }
slouken@12534
   182
sylvain@12538
   183
    return;
slouken@12534
   184
}
slouken@12534
   185
sylvain@12540
   186
/* this callback handler is called every time a buffer finishes playing */
sylvain@12538
   187
static void
sylvain@12538
   188
bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
slouken@12534
   189
{
sylvain@12540
   190
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) context;
sylvain@12538
   191
    static int t = 0;
sylvain@12538
   192
sylvain@12540
   193
    /* assert(bq == bqPlayerBufferQueue); */
sylvain@12540
   194
    /* assert(NULL == context); */
slouken@12534
   195
sylvain@12540
   196
    /* for streaming playback, replace this test by logic to find and fill the next buffer */
slouken@12534
   197
#if 0
sylvain@12538
   198
    if (--nextCount > 0 && NULL != nextBuffer && 0 != nextSize)
sylvain@12538
   199
    {
sylvain@12538
   200
        SLresult result;
sylvain@12538
   201
sylvain@12540
   202
        /* enqueue another buffer */
sylvain@12538
   203
        result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
sylvain@12540
   204
        /* the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT, */
sylvain@12540
   205
        /* which for this code example would indicate a programming error */
sylvain@12538
   206
        assert(SL_RESULT_SUCCESS == result);
sylvain@12538
   207
        (void) result;
sylvain@12538
   208
    }
slouken@12534
   209
#endif
sylvain@12538
   210
    LOGI("SLES: Playback Callmeback %u", t++);
sylvain@12540
   211
    SDL_SemPost(audiodata->playsem);
sylvain@12538
   212
    return;
slouken@12534
   213
}
slouken@12534
   214
sylvain@12538
   215
static int
sylvain@12538
   216
openslES_CreatePCMRecorder(_THIS)
slouken@12534
   217
{
sylvain@12540
   218
/*    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   219
sylvain@12538
   220
    LOGE("openslES_CreatePCMRecorder not implimented yet!");
sylvain@12538
   221
    return SDL_SetError("openslES_CreatePCMRecorder not implimented yet!");
slouken@12534
   222
}
slouken@12534
   223
sylvain@12538
   224
static void
sylvain@12540
   225
openslES_DestroyPCMRecorder(_THIS)
slouken@12534
   226
{
sylvain@12540
   227
/*    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   228
sylvain@12538
   229
    return;
slouken@12534
   230
}
slouken@12534
   231
sylvain@12538
   232
static int
sylvain@12539
   233
openslES_CreatePCMPlayer(_THIS)
slouken@12534
   234
{
sylvain@12540
   235
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   236
sylvain@12538
   237
    SLDataFormat_PCM format_pcm;
sylvain@12538
   238
sylvain@12538
   239
    SLresult result;
sylvain@12538
   240
    int i;
sylvain@12538
   241
sylvain@12540
   242
#if 0
sylvain@12538
   243
      SDL_AudioFormat test_format;
sylvain@12538
   244
      test_format = SDL_FirstAudioFormat( this->spec.format );
sylvain@12538
   245
sylvain@12538
   246
      while (test_format != 0) {
sylvain@12538
   247
sylvain@12538
   248
          if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
sylvain@12538
   249
              break;
sylvain@12538
   250
          }
sylvain@12538
   251
          test_format = SDL_NextAudioFormat();
sylvain@12538
   252
      }
sylvain@12538
   253
sylvain@12538
   254
      if ( test_format == 0 ) {
sylvain@12540
   255
          /* Didn't find a compatible format : */
sylvain@12538
   256
          LOGI( "No compatible audio format!" );
sylvain@12538
   257
          return SDL_SetError("No compatible audio format!");
sylvain@12538
   258
      }
sylvain@12538
   259
sylvain@12538
   260
      this->spec.format = test_format;
sylvain@12540
   261
#endif
sylvain@12538
   262
sylvain@12540
   263
    /* Update the fragment size as size in bytes */
sylvain@12538
   264
    SDL_CalculateAudioSpec(&this->spec);
sylvain@12538
   265
sylvain@12538
   266
    LOGI("Try to open %u hz %u bit chan %u %s samples %u",
sylvain@12538
   267
          this->spec.freq, SDL_AUDIO_BITSIZE(this->spec.format),
sylvain@12538
   268
          this->spec.channels, (test_format & 0x1000) ? "BE" : "LE", this->spec.samples);
sylvain@12538
   269
sylvain@12540
   270
    /* configure audio source */
sylvain@12538
   271
    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
sylvain@12540
   272
    /* SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, OPENSLES_BUFFERS }; */
sylvain@12538
   273
sylvain@12538
   274
    format_pcm.formatType    = SL_DATAFORMAT_PCM;
sylvain@12538
   275
    format_pcm.numChannels   = this->spec.channels;
sylvain@12540
   276
    format_pcm.samplesPerSec = this->spec.freq * 1000;  /* / kilo Hz to milli Hz */
sylvain@12538
   277
    format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
sylvain@12538
   278
    format_pcm.containerSize = SDL_AUDIO_BITSIZE(this->spec.format);
sylvain@12538
   279
sylvain@12538
   280
    if (SDL_AUDIO_ISBIGENDIAN(this->spec.format)) {
sylvain@12538
   281
        format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
sylvain@12538
   282
    } else {
sylvain@12538
   283
        format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
sylvain@12538
   284
    }
slouken@12534
   285
slouken@12534
   286
/*
sylvain@12538
   287
#define SL_SPEAKER_FRONT_LEFT            ((SLuint32) 0x00000001)
sylvain@12538
   288
#define SL_SPEAKER_FRONT_RIGHT           ((SLuint32) 0x00000002)
sylvain@12538
   289
#define SL_SPEAKER_FRONT_CENTER          ((SLuint32) 0x00000004)
sylvain@12538
   290
#define SL_SPEAKER_LOW_FREQUENCY         ((SLuint32) 0x00000008)
sylvain@12538
   291
#define SL_SPEAKER_BACK_LEFT             ((SLuint32) 0x00000010)
sylvain@12538
   292
#define SL_SPEAKER_BACK_RIGHT            ((SLuint32) 0x00000020)
sylvain@12538
   293
#define SL_SPEAKER_FRONT_LEFT_OF_CENTER  ((SLuint32) 0x00000040)
sylvain@12538
   294
#define SL_SPEAKER_FRONT_RIGHT_OF_CENTER ((SLuint32) 0x00000080)
sylvain@12538
   295
#define SL_SPEAKER_BACK_CENTER           ((SLuint32) 0x00000100)
sylvain@12538
   296
#define SL_SPEAKER_SIDE_LEFT             ((SLuint32) 0x00000200)
sylvain@12538
   297
#define SL_SPEAKER_SIDE_RIGHT            ((SLuint32) 0x00000400)
sylvain@12538
   298
#define SL_SPEAKER_TOP_CENTER            ((SLuint32) 0x00000800)
sylvain@12538
   299
#define SL_SPEAKER_TOP_FRONT_LEFT        ((SLuint32) 0x00001000)
sylvain@12538
   300
#define SL_SPEAKER_TOP_FRONT_CENTER      ((SLuint32) 0x00002000)
sylvain@12538
   301
#define SL_SPEAKER_TOP_FRONT_RIGHT       ((SLuint32) 0x00004000)
sylvain@12538
   302
#define SL_SPEAKER_TOP_BACK_LEFT         ((SLuint32) 0x00008000)
sylvain@12538
   303
#define SL_SPEAKER_TOP_BACK_CENTER       ((SLuint32) 0x00010000)
sylvain@12538
   304
#define SL_SPEAKER_TOP_BACK_RIGHT        ((SLuint32) 0x00020000)
slouken@12534
   305
*/
slouken@12534
   306
sylvain@12538
   307
    if (this->spec.channels == 1) {
sylvain@12538
   308
        format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
sylvain@12538
   309
    } else if (this->spec.channels == 2) {
sylvain@12538
   310
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
sylvain@12538
   311
    } else if (this->spec.channels == 3) {
sylvain@12538
   312
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER;
sylvain@12538
   313
    } else if (this->spec.channels == 4) {
sylvain@12538
   314
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
sylvain@12538
   315
              SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
sylvain@12538
   316
    } else {
sylvain@12538
   317
        format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
sylvain@12538
   318
              SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT | SL_SPEAKER_FRONT_CENTER;
sylvain@12538
   319
    }
slouken@12534
   320
sylvain@12538
   321
    SLDataSource audioSrc = { &loc_bufq, &format_pcm };
slouken@12534
   322
sylvain@12540
   323
    /* configure audio sink */
sylvain@12538
   324
    SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
sylvain@12538
   325
    SLDataSink audioSnk = { &loc_outmix, NULL };
slouken@12534
   326
sylvain@12540
   327
    /* create audio player */
sylvain@12538
   328
    const SLInterfaceID ids[2] = {
sylvain@12538
   329
        SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
sylvain@12538
   330
        SL_IID_VOLUME
sylvain@12538
   331
    };
slouken@12534
   332
sylvain@12538
   333
    const SLboolean req[2] = {
sylvain@12538
   334
        SL_BOOLEAN_TRUE,
sylvain@12538
   335
        SL_BOOLEAN_FALSE,
sylvain@12538
   336
    };
slouken@12534
   337
sylvain@12538
   338
    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 2, ids, req);
sylvain@12538
   339
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   340
        LOGE("CreateAudioPlayer failed");
sylvain@12538
   341
        goto failed;
sylvain@12538
   342
    }
slouken@12534
   343
sylvain@12540
   344
    /* realize the player */
sylvain@12538
   345
    result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
sylvain@12538
   346
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   347
        LOGE("RealizeAudioPlayer failed");
sylvain@12538
   348
        goto failed;
sylvain@12538
   349
    }
slouken@12534
   350
sylvain@12540
   351
    /* get the play interface */
sylvain@12538
   352
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
sylvain@12538
   353
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   354
        LOGE("SL_IID_PLAY interface get failed");
sylvain@12538
   355
        goto failed;
sylvain@12538
   356
    }
slouken@12534
   357
sylvain@12540
   358
    /* get the buffer queue interface */
sylvain@12538
   359
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bqPlayerBufferQueue);
sylvain@12538
   360
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   361
        LOGE("SL_IID_BUFFERQUEUE interface get failed");
sylvain@12538
   362
        goto failed;
sylvain@12538
   363
    }
slouken@12534
   364
sylvain@12540
   365
    /* register callback on the buffer queue */
sylvain@12540
   366
    /* context is '(SDL_PrivateAudioData *)this->hidden' */
sylvain@12540
   367
    result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this->hidden);
sylvain@12538
   368
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   369
        LOGE("RegisterCallback failed");
sylvain@12538
   370
        goto failed;
sylvain@12538
   371
    }
slouken@12534
   372
slouken@12534
   373
#if 0
sylvain@12540
   374
    /* get the effect send interface */
sylvain@12538
   375
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend);
sylvain@12538
   376
    if (SL_RESULT_SUCCESS != result)
sylvain@12538
   377
    {
slouken@12534
   378
sylvain@12538
   379
        LOGE("SL_IID_EFFECTSEND interface get failed");
sylvain@12538
   380
        goto failed;
sylvain@12538
   381
    }
slouken@12534
   382
#endif
slouken@12534
   383
sylvain@12540
   384
#if 0   /* mute/solo is not supported for sources that are known to be mono, as this is */
sylvain@12540
   385
    /* get the mute/solo interface */
slouken@12534
   386
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_MUTESOLO, &bqPlayerMuteSolo);
slouken@12534
   387
    assert(SL_RESULT_SUCCESS == result);
sylvain@12538
   388
    (void) result;
slouken@12534
   389
#endif
slouken@12534
   390
sylvain@12540
   391
    /* get the volume interface */
sylvain@12538
   392
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
sylvain@12538
   393
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   394
        LOGE("SL_IID_VOLUME interface get failed");
sylvain@12540
   395
        /* goto failed; */
sylvain@12538
   396
    }
slouken@12534
   397
sylvain@12540
   398
    /* set the player's state to playing */
sylvain@12538
   399
    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
sylvain@12538
   400
    if (SL_RESULT_SUCCESS != result) {
sylvain@12538
   401
        LOGE("Play set state failed");
sylvain@12538
   402
        goto failed;
sylvain@12538
   403
    }
slouken@12534
   404
slouken@12534
   405
    /* Create the audio buffer semaphore */
sylvain@12540
   406
    audiodata->playsem = SDL_CreateSemaphore(NUM_BUFFERS - 1);
sylvain@12540
   407
    if (!audiodata->playsem) {
sylvain@12538
   408
        LOGE("cannot create Semaphore!");
sylvain@12538
   409
        goto failed;
sylvain@12538
   410
    }
slouken@12534
   411
sylvain@12538
   412
    /* Create the sound buffers */
sylvain@12540
   413
    audiodata->mixbuff = (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
sylvain@12540
   414
    if (audiodata->mixbuff == NULL) {
sylvain@12538
   415
        LOGE("mixbuffer allocate - out of memory");
sylvain@12538
   416
        goto failed;
sylvain@12538
   417
    }
slouken@12534
   418
sylvain@12538
   419
    for (i = 0; i < NUM_BUFFERS; i++) {
sylvain@12540
   420
        audiodata->pmixbuff[i] = audiodata->mixbuff + i * this->spec.size;
sylvain@12538
   421
    }
slouken@12534
   422
slouken@12534
   423
    return 0;
slouken@12534
   424
sylvain@12538
   425
failed:
slouken@12534
   426
sylvain@12540
   427
    openslES_DestroyPCMPlayer(this);
slouken@12534
   428
sylvain@12538
   429
    return SDL_SetError("Open device failed!");
slouken@12534
   430
}
slouken@12534
   431
sylvain@12538
   432
static void
sylvain@12540
   433
openslES_DestroyPCMPlayer(_THIS)
slouken@12534
   434
{
sylvain@12540
   435
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   436
sylvain@12540
   437
    /* destroy buffer queue audio player object, and invalidate all associated interfaces */
sylvain@12538
   438
    if (bqPlayerObject != NULL) {
slouken@12534
   439
sylvain@12538
   440
        (*bqPlayerObject)->Destroy(bqPlayerObject);
slouken@12534
   441
sylvain@12538
   442
        bqPlayerObject = NULL;
sylvain@12538
   443
        bqPlayerPlay = NULL;
slouken@12534
   444
        bqPlayerBufferQueue = NULL;
sylvain@12540
   445
        /* bqPlayerEffectSend = NULL; */
sylvain@12538
   446
        bqPlayerMuteSolo = NULL;
sylvain@12538
   447
        bqPlayerVolume = NULL;
slouken@12534
   448
    }
slouken@12534
   449
sylvain@12540
   450
    if (audiodata->playsem) {
sylvain@12540
   451
        SDL_DestroySemaphore(audiodata->playsem);
sylvain@12540
   452
        audiodata->playsem = NULL;
sylvain@12538
   453
    }
slouken@12534
   454
sylvain@12540
   455
    if (audiodata->mixbuff) {
sylvain@12540
   456
        SDL_free(audiodata->mixbuff);
sylvain@12538
   457
    }
slouken@12534
   458
sylvain@12538
   459
    return;
slouken@12534
   460
}
slouken@12534
   461
sylvain@12538
   462
static int
sylvain@12538
   463
openslES_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
slouken@12534
   464
{
sylvain@12539
   465
    this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
sylvain@12539
   466
    if (this->hidden == NULL) {
sylvain@12539
   467
        return SDL_OutOfMemory();
sylvain@12539
   468
    }
sylvain@12539
   469
sylvain@12538
   470
    if (iscapture) {
sylvain@12538
   471
        LOGI("openslES_OpenDevice( ) %s for capture", devname);
sylvain@12538
   472
        return openslES_CreatePCMRecorder(this);
sylvain@12538
   473
    } else {
sylvain@12538
   474
        LOGI("openslES_OpenDevice( ) %s for playing", devname);
sylvain@12538
   475
        return openslES_CreatePCMPlayer(this);
sylvain@12538
   476
    }
slouken@12534
   477
}
slouken@12534
   478
sylvain@12538
   479
static void
sylvain@12538
   480
openslES_CloseDevice(_THIS)
slouken@12534
   481
{
sylvain@12540
   482
    /* struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
sylvain@12540
   483
sylvain@12538
   484
    if (this->iscapture) {
sylvain@12538
   485
        LOGI("openslES_CloseDevice( ) for capture");
sylvain@12540
   486
        openslES_DestroyPCMRecorder(this);
sylvain@12538
   487
    } else {
sylvain@12538
   488
        LOGI("openslES_CloseDevice( ) for playing");
sylvain@12540
   489
        openslES_DestroyPCMPlayer(this);
sylvain@12538
   490
    }
sylvain@12540
   491
    
sylvain@12539
   492
    SDL_free(this->hidden);
sylvain@12539
   493
sylvain@12538
   494
    return;
slouken@12534
   495
}
slouken@12534
   496
sylvain@12538
   497
static void
sylvain@12538
   498
openslES_WaitDevice(_THIS)
slouken@12534
   499
{
sylvain@12540
   500
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   501
sylvain@12538
   502
    LOGI("openslES_WaitDevice( )");
slouken@12534
   503
slouken@12534
   504
    /* Wait for an audio chunk to finish */
sylvain@12540
   505
    /* WaitForSingleObject(this->hidden->audio_sem, INFINITE); */
sylvain@12540
   506
    SDL_SemWait(audiodata->playsem);
slouken@12534
   507
sylvain@12538
   508
    return;
slouken@12534
   509
}
slouken@12534
   510
sylvain@12540
   511
/*/           n   playn sem */
sylvain@12540
   512
/* getbuf     0   -     1 */
sylvain@12540
   513
/* fill buff  0   -     1 */
sylvain@12540
   514
/* play       0 - 0     1 */
sylvain@12540
   515
/* wait       1   0     0 */
sylvain@12540
   516
/* getbuf     1   0     0 */
sylvain@12540
   517
/* fill buff  1   0     0 */
sylvain@12540
   518
/* play       0   0     0 */
sylvain@12540
   519
/* wait */
sylvain@12540
   520
/* */
sylvain@12540
   521
/* okay.. */
slouken@12534
   522
sylvain@12538
   523
static Uint8 *
sylvain@12538
   524
openslES_GetDeviceBuf(_THIS)
slouken@12534
   525
{
sylvain@12540
   526
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12540
   527
sylvain@12538
   528
    LOGI("openslES_GetDeviceBuf( )");
sylvain@12540
   529
    return audiodata->pmixbuff[audiodata->next_buffer];
slouken@12534
   530
}
slouken@12534
   531
sylvain@12538
   532
static void
sylvain@12538
   533
openslES_PlayDevice(_THIS)
slouken@12534
   534
{
sylvain@12540
   535
    struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
sylvain@12538
   536
    SLresult result;
slouken@12534
   537
sylvain@12538
   538
    LOGI("======openslES_PlayDevice( )======");
slouken@12534
   539
    /* Queue it up */
slouken@12534
   540
sylvain@12540
   541
    result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
sylvain@12538
   542
    if (SL_RESULT_SUCCESS != result) {
sylvain@12540
   543
        /* just puk here */
sylvain@12540
   544
        /* next ! */
sylvain@12538
   545
    }
slouken@12534
   546
sylvain@12540
   547
    audiodata->next_buffer++;
sylvain@12540
   548
    if (audiodata->next_buffer >= NUM_BUFFERS) {
sylvain@12540
   549
        audiodata->next_buffer = 0;
sylvain@12538
   550
    }
slouken@12534
   551
sylvain@12538
   552
    return;
slouken@12534
   553
}
slouken@12534
   554
sylvain@12538
   555
static int
sylvain@12538
   556
openslES_Init(SDL_AudioDriverImpl * impl)
slouken@12534
   557
{
sylvain@12538
   558
    LOGI("openslES_Init() called");
slouken@12534
   559
sylvain@12538
   560
    if (!openslES_CreateEngine()) {
sylvain@12538
   561
        return 0;
sylvain@12538
   562
    }
slouken@12534
   563
sylvain@12538
   564
    LOGI("openslES_Init() - set pointers");
slouken@12534
   565
sylvain@12538
   566
    /* Set the function pointers */
sylvain@12540
   567
    /* impl->DetectDevices = openslES_DetectDevices; */
sylvain@12538
   568
    impl->OpenDevice    = openslES_OpenDevice;
sylvain@12539
   569
    impl->CloseDevice   = openslES_CloseDevice;
sylvain@12538
   570
    impl->PlayDevice    = openslES_PlayDevice;
sylvain@12538
   571
    impl->GetDeviceBuf  = openslES_GetDeviceBuf;
sylvain@12538
   572
    impl->Deinitialize  = openslES_DestroyEngine;
sylvain@12538
   573
    impl->WaitDevice    = openslES_WaitDevice;
slouken@12534
   574
sylvain@12538
   575
    /* and the capabilities */
sylvain@12538
   576
    impl->HasCaptureSupport             = 0;        /* TODO */
sylvain@12538
   577
    impl->OnlyHasDefaultOutputDevice    = 1;
sylvain@12540
   578
    /* impl->OnlyHasDefaultInputDevice  = 1; */
slouken@12534
   579
sylvain@12538
   580
    LOGI("openslES_Init() - succes");
slouken@12534
   581
sylvain@12538
   582
    /* this audio target is available. */
sylvain@12538
   583
    return 1;
slouken@12534
   584
}
slouken@12534
   585
slouken@12534
   586
AudioBootStrap openslES_bootstrap = {
sylvain@12538
   587
    "openslES", "opensl ES audio driver", openslES_Init, 0
slouken@12534
   588
};
slouken@12534
   589
slouken@12534
   590
#endif /* SDL_AUDIO_DRIVER_OPENSLES */
slouken@12534
   591
slouken@12534
   592
/* vi: set ts=4 sw=4 expandtab: */