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