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