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