src/audio/openslES/SDL_openslES.c
changeset 12534 682d9b5ecbed
child 12538 fad97a498ffb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/openslES/SDL_openslES.c	Sat Jan 12 12:18:44 2019 -0800
     1.3 @@ -0,0 +1,573 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_AUDIO_DRIVER_OPENSLES
    1.27 +
    1.28 +#include "SDL_audio.h"
    1.29 +#include "../SDL_audio_c.h"
    1.30 +#include "SDL_openslES.h"
    1.31 +
    1.32 +// for native audio
    1.33 +#include <SLES/OpenSLES.h>
    1.34 +#include <SLES/OpenSLES_Android.h>
    1.35 +
    1.36 +#include <android/log.h>
    1.37 +
    1.38 +#define LOG_TAG "SDL_openslES"
    1.39 +
    1.40 +//#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
    1.41 +//#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    1.42 +//#define LOGI(...) do {} while (0)
    1.43 +//#define LOGE(...) do {} while (0)
    1.44 +#define LOGI(...)
    1.45 +#define LOGE(...)
    1.46 +
    1.47 +// engine interfaces
    1.48 +static SLObjectItf engineObject = NULL;
    1.49 +static SLEngineItf engineEngine;
    1.50 +
    1.51 +// output mix interfaces
    1.52 +static SLObjectItf outputMixObject = NULL;
    1.53 +//static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
    1.54 +
    1.55 +// aux effect on the output mix, used by the buffer queue player
    1.56 +static const SLEnvironmentalReverbSettings reverbSettings =
    1.57 +    SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;
    1.58 +
    1.59 +// buffer queue player interfaces
    1.60 +static SLObjectItf						bqPlayerObject = NULL;
    1.61 +static SLPlayItf 						bqPlayerPlay;
    1.62 +static SLAndroidSimpleBufferQueueItf	bqPlayerBufferQueue;
    1.63 +//static SLEffectSendItf					bqPlayerEffectSend;
    1.64 +static SLMuteSoloItf					bqPlayerMuteSolo;
    1.65 +static SLVolumeItf						bqPlayerVolume;
    1.66 +
    1.67 +// recorder interfaces TODO
    1.68 +static SLObjectItf						recorderObject = NULL;
    1.69 +static SLRecordItf						recorderRecord;
    1.70 +static SLAndroidSimpleBufferQueueItf	recorderBufferQueue;
    1.71 +
    1.72 +// pointer and size of the next player buffer to enqueue, and number of remaining buffers
    1.73 +static short 	*nextBuffer;
    1.74 +static unsigned	nextSize;
    1.75 +static int 		nextCount;
    1.76 +
    1.77 +static const char	*sldevaudiorecorderstr	= "SLES Audio Recorder";
    1.78 +static const char	*sldevaudioplayerstr 	= "SLES Audio Player";
    1.79 +
    1.80 +#define	SLES_DEV_AUDIO_RECORDER	sldevaudiorecorderstr
    1.81 +#define	SLES_DEV_AUDIO_PLAYER	sldevaudioplayerstr
    1.82 +
    1.83 +#define NUM_BUFFERS 2           /* -- Don't lower this! */
    1.84 +
    1.85 +static	Uint8	*mixbuff = NULL;
    1.86 +static	int		next_buffer = 0;
    1.87 +static	Uint8	*pmixbuff[NUM_BUFFERS];
    1.88 +
    1.89 +static SDL_sem *playsem = NULL, *recsem = NULL;
    1.90 +
    1.91 +//static SDL_AudioDevice* audioDevice = NULL;
    1.92 +
    1.93 +#if 0
    1.94 +static void openslES_DetectDevices( int iscapture )
    1.95 +{
    1.96 +	LOGI( "openSLES_DetectDevices()" );
    1.97 +    if ( iscapture )
    1.98 +            addfn( SLES_DEV_AUDIO_RECORDER );
    1.99 +	else
   1.100 +            addfn( SLES_DEV_AUDIO_PLAYER );
   1.101 +	return;
   1.102 +}
   1.103 +#endif
   1.104 +
   1.105 +static void openslES_DestroyEngine( void );
   1.106 +
   1.107 +static int	openslES_CreateEngine( void )
   1.108 +{
   1.109 +	SLresult result;
   1.110 +
   1.111 +	LOGI( "openSLES_CreateEngine()" );
   1.112 +
   1.113 +	// create engine
   1.114 +	result = slCreateEngine( &engineObject, 0, NULL, 0, NULL, NULL );
   1.115 +	if ( SL_RESULT_SUCCESS != result ) {
   1.116 +
   1.117 +		LOGE( "slCreateEngine failed" );
   1.118 +		goto error;
   1.119 +	}
   1.120 +
   1.121 +	LOGI( "slCreateEngine OK" );
   1.122 +
   1.123 +    // realize the engine
   1.124 +    result = (*engineObject)->Realize( engineObject, SL_BOOLEAN_FALSE );
   1.125 +	if ( SL_RESULT_SUCCESS != result ) {
   1.126 +
   1.127 +		LOGE( "RealizeEngine failed" );
   1.128 +		goto error;
   1.129 +	}
   1.130 +
   1.131 +	LOGI( "RealizeEngine OK" );
   1.132 +
   1.133 +    // get the engine interface, which is needed in order to create other objects
   1.134 +    result = (*engineObject)->GetInterface( engineObject, SL_IID_ENGINE, &engineEngine );
   1.135 +	if ( SL_RESULT_SUCCESS != result ) {
   1.136 +
   1.137 +		LOGE( "EngineGetInterface failed" );
   1.138 +		goto error;
   1.139 +	}
   1.140 +
   1.141 +	LOGI( "EngineGetInterface OK" );
   1.142 +
   1.143 +    // create output mix, with environmental reverb specified as a non-required interface
   1.144 +//  const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB };
   1.145 +//  const SLboolean req[1] = { SL_BOOLEAN_FALSE };
   1.146 +
   1.147 +  const SLInterfaceID ids[1] = { SL_IID_VOLUME };
   1.148 +  const SLboolean req[1] = { SL_BOOLEAN_FALSE };
   1.149 +  result = (*engineEngine)->CreateOutputMix( engineEngine, &outputMixObject, 1, ids, req );
   1.150 +
   1.151 +	if ( SL_RESULT_SUCCESS != result ) {
   1.152 +
   1.153 +		LOGE( "CreateOutputMix failed" );
   1.154 +		goto error;
   1.155 +	}
   1.156 +	LOGI( "CreateOutputMix OK" );
   1.157 +
   1.158 +  // realize the output mix
   1.159 +  result = (*outputMixObject)->Realize( outputMixObject, SL_BOOLEAN_FALSE );
   1.160 +	if ( SL_RESULT_SUCCESS != result ) {
   1.161 +
   1.162 +		LOGE( "RealizeOutputMix failed" );
   1.163 +		goto error;
   1.164 +	}
   1.165 +
   1.166 +	return 1;
   1.167 +
   1.168 +error:;
   1.169 +	openslES_DestroyEngine( );
   1.170 +	return 0;
   1.171 +}
   1.172 +
   1.173 +static	void	openslES_DestroyPCMPlayer( void );
   1.174 +static	void	openslES_DestroyPCMRecorder( void );
   1.175 +
   1.176 +static void openslES_DestroyEngine( void )
   1.177 +{
   1.178 +	LOGI( "openslES_DestroyEngine()" );
   1.179 +
   1.180 +	openslES_DestroyPCMPlayer( );
   1.181 +	openslES_DestroyPCMRecorder( );
   1.182 +
   1.183 +	// destroy output mix object, and invalidate all associated interfaces
   1.184 +	if ( outputMixObject != NULL ) {
   1.185 +
   1.186 +	    (*outputMixObject)->Destroy( outputMixObject );
   1.187 +	    outputMixObject = NULL;
   1.188 +//	    outputMixEnvironmentalReverb = NULL;
   1.189 +	}
   1.190 +
   1.191 +	// destroy engine object, and invalidate all associated interfaces
   1.192 +	if (engineObject != NULL) {
   1.193 +
   1.194 +	    (*engineObject)->Destroy( engineObject );
   1.195 +	    engineObject = NULL;
   1.196 +	    engineEngine = NULL;
   1.197 +	}
   1.198 +
   1.199 +	return;
   1.200 +}
   1.201 +
   1.202 +// this callback handler is called every time a buffer finishes playing
   1.203 +static void bqPlayerCallback( SLAndroidSimpleBufferQueueItf bq, void *context )
   1.204 +{
   1.205 +	static int	t = 0;
   1.206 +//    assert(bq == bqPlayerBufferQueue);
   1.207 +//    assert(NULL == context);
   1.208 +
   1.209 +    // for streaming playback, replace this test by logic to find and fill the next buffer
   1.210 +#if 0
   1.211 +	if (--nextCount > 0 && NULL != nextBuffer && 0 != nextSize) {
   1.212 +		SLresult result;
   1.213 +		// enqueue another buffer
   1.214 +		result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
   1.215 +		// the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
   1.216 +		// which for this code example would indicate a programming error
   1.217 +		assert(SL_RESULT_SUCCESS == result);
   1.218 +		(void)result;
   1.219 +	}
   1.220 +#endif
   1.221 +
   1.222 +	LOGI( "SLES: Playback Callmeback %u", t++ );
   1.223 +
   1.224 +	SDL_SemPost( playsem );
   1.225 +
   1.226 +	return;
   1.227 +}
   1.228 +
   1.229 +static int openslES_CreatePCMRecorder( _THIS )
   1.230 +{
   1.231 +	LOGE( "openslES_CreatePCMRecorder not implimented yet!" );
   1.232 +	return SDL_SetError( "openslES_CreatePCMRecorder not implimented yet!" );
   1.233 +}
   1.234 +
   1.235 +static void openslES_DestroyPCMRecorder( void )
   1.236 +{
   1.237 +	return;
   1.238 +}
   1.239 +
   1.240 +static int openslES_CreatePCMPlayer( _THIS )
   1.241 +{
   1.242 +	SLDataFormat_PCM format_pcm;
   1.243 +  SDL_AudioFormat	test_format;
   1.244 +	SLresult result;
   1.245 +	int	i;
   1.246 +
   1.247 +/*
   1.248 +	test_format = SDL_FirstAudioFormat( this->spec.format );
   1.249 +
   1.250 +	while ( test_format != 0 ) {
   1.251 +
   1.252 +		if ( SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format ) ) break;
   1.253 +		test_format = SDL_NextAudioFormat( );
   1.254 +	}
   1.255 +
   1.256 +	if ( test_format == 0 ) {
   1.257 +
   1.258 +		// Didn't find a compatible format :( 
   1.259 +		LOGI( "No compatible audio format!" );
   1.260 +		return SDL_SetError("No compatible audio format!");
   1.261 +	}
   1.262 +
   1.263 +	this->spec.format = test_format;
   1.264 +*/
   1.265 +
   1.266 + // Update the fragment size as size in bytes 
   1.267 + SDL_CalculateAudioSpec( &this->spec );
   1.268 +
   1.269 +	LOGI( "Try to open %u hz %u bit chan %u %s samples %u", 
   1.270 +			this->spec.freq, SDL_AUDIO_BITSIZE( this->spec.format ), 
   1.271 +			this->spec.channels, (test_format&0x1000) ? "BE" : "LE", this->spec.samples 
   1.272 +		);
   1.273 +
   1.274 +	// configure audio source
   1.275 +	SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
   1.276 +//	SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, OPENSLES_BUFFERS };
   1.277 +
   1.278 +	format_pcm.formatType    = SL_DATAFORMAT_PCM;
   1.279 +	format_pcm.numChannels   = this->spec.channels;
   1.280 +	format_pcm.samplesPerSec = this->spec.freq * 1000;	/// kilo Hz to milli Hz
   1.281 +	format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE( this->spec.format );
   1.282 +	format_pcm.containerSize = SDL_AUDIO_BITSIZE( this->spec.format );
   1.283 +
   1.284 +	if ( SDL_AUDIO_ISBIGENDIAN( this->spec.format ) )
   1.285 +		format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
   1.286 +	else
   1.287 +		format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
   1.288 +
   1.289 +/*
   1.290 +#define SL_SPEAKER_FRONT_LEFT			((SLuint32) 0x00000001)
   1.291 +#define SL_SPEAKER_FRONT_RIGHT			((SLuint32) 0x00000002)
   1.292 +#define SL_SPEAKER_FRONT_CENTER			((SLuint32) 0x00000004)
   1.293 +#define SL_SPEAKER_LOW_FREQUENCY			((SLuint32) 0x00000008)
   1.294 +#define SL_SPEAKER_BACK_LEFT			((SLuint32) 0x00000010)
   1.295 +#define SL_SPEAKER_BACK_RIGHT			((SLuint32) 0x00000020)
   1.296 +#define SL_SPEAKER_FRONT_LEFT_OF_CENTER	((SLuint32) 0x00000040)
   1.297 +#define SL_SPEAKER_FRONT_RIGHT_OF_CENTER	((SLuint32) 0x00000080)
   1.298 +#define SL_SPEAKER_BACK_CENTER			((SLuint32) 0x00000100)
   1.299 +#define SL_SPEAKER_SIDE_LEFT			((SLuint32) 0x00000200)
   1.300 +#define SL_SPEAKER_SIDE_RIGHT			((SLuint32) 0x00000400)
   1.301 +#define SL_SPEAKER_TOP_CENTER			((SLuint32) 0x00000800)
   1.302 +#define SL_SPEAKER_TOP_FRONT_LEFT		((SLuint32) 0x00001000)
   1.303 +#define SL_SPEAKER_TOP_FRONT_CENTER		((SLuint32) 0x00002000)
   1.304 +#define SL_SPEAKER_TOP_FRONT_RIGHT		((SLuint32) 0x00004000)
   1.305 +#define SL_SPEAKER_TOP_BACK_LEFT			((SLuint32) 0x00008000)
   1.306 +#define SL_SPEAKER_TOP_BACK_CENTER		((SLuint32) 0x00010000)
   1.307 +#define SL_SPEAKER_TOP_BACK_RIGHT		((SLuint32) 0x00020000)
   1.308 +*/
   1.309 +
   1.310 +	if ( this->spec.channels == 1 )
   1.311 +		format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
   1.312 +	else if ( this->spec.channels == 2 )
   1.313 +		format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
   1.314 +	else if ( this->spec.channels == 3 )
   1.315 +		format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER;
   1.316 +	else if ( this->spec.channels == 4 )
   1.317 +		format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
   1.318 +								 SL_SPEAKER_BACK_LEFT  | SL_SPEAKER_BACK_RIGHT;
   1.319 +	else
   1.320 +		format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
   1.321 +								 SL_SPEAKER_BACK_LEFT  | SL_SPEAKER_BACK_RIGHT |
   1.322 +								 SL_SPEAKER_FRONT_CENTER;
   1.323 +
   1.324 +	SLDataSource audioSrc = { &loc_bufq, &format_pcm };
   1.325 +
   1.326 +	// configure audio sink
   1.327 +	SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
   1.328 +	SLDataSink audioSnk = { &loc_outmix, NULL };
   1.329 +
   1.330 +    // create audio player
   1.331 +    const SLInterfaceID ids[2] = { 
   1.332 +			SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
   1.333 +			SL_IID_VOLUME 
   1.334 +			};
   1.335 +
   1.336 +    const SLboolean req[2] = { 
   1.337 +			SL_BOOLEAN_TRUE, 
   1.338 +			SL_BOOLEAN_FALSE,
   1.339 +			};
   1.340 +
   1.341 +	result = (*engineEngine)->CreateAudioPlayer( engineEngine, &bqPlayerObject, &audioSrc, &audioSnk,
   1.342 +				2, ids, req );
   1.343 +	if ( SL_RESULT_SUCCESS != result ) {
   1.344 +
   1.345 +		LOGE( "CreateAudioPlayer failed" );
   1.346 +		goto failed;
   1.347 +	}
   1.348 +
   1.349 +    // realize the player
   1.350 +	result = (*bqPlayerObject)->Realize( bqPlayerObject, SL_BOOLEAN_FALSE );
   1.351 +	if ( SL_RESULT_SUCCESS != result ) {
   1.352 +
   1.353 +		LOGE( "RealizeAudioPlayer failed" );
   1.354 +		goto failed;
   1.355 +	}
   1.356 +
   1.357 +	// get the play interface
   1.358 +	result = (*bqPlayerObject)->GetInterface( bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay );
   1.359 +	if ( SL_RESULT_SUCCESS != result ) {
   1.360 +
   1.361 +		LOGE( "SL_IID_PLAY interface get failed" );
   1.362 +		goto failed;
   1.363 +	}
   1.364 +
   1.365 +    // get the buffer queue interface
   1.366 +	result = (*bqPlayerObject)->GetInterface( bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bqPlayerBufferQueue );
   1.367 +	if ( SL_RESULT_SUCCESS != result ) {
   1.368 +
   1.369 +		LOGE( "SL_IID_BUFFERQUEUE interface get failed" );
   1.370 +		goto failed;
   1.371 +	}
   1.372 +
   1.373 +	// register callback on the buffer queue
   1.374 +	result = (*bqPlayerBufferQueue)->RegisterCallback( bqPlayerBufferQueue, bqPlayerCallback, NULL );
   1.375 +	if ( SL_RESULT_SUCCESS != result ) {
   1.376 +
   1.377 +		LOGE( "RegisterCallback failed" );
   1.378 +		goto failed;
   1.379 +	}
   1.380 +
   1.381 +#if 0
   1.382 +    // get the effect send interface
   1.383 +	result = (*bqPlayerObject)->GetInterface( bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend );
   1.384 +	if ( SL_RESULT_SUCCESS != result ) {
   1.385 +
   1.386 +		LOGE( "SL_IID_EFFECTSEND interface get failed" );
   1.387 +		goto failed;
   1.388 +	}
   1.389 +#endif
   1.390 +
   1.391 +#if 0   // mute/solo is not supported for sources that are known to be mono, as this is
   1.392 +    // get the mute/solo interface
   1.393 +    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_MUTESOLO, &bqPlayerMuteSolo);
   1.394 +    assert(SL_RESULT_SUCCESS == result);
   1.395 +    (void)result;
   1.396 +#endif
   1.397 +
   1.398 +    // get the volume interface
   1.399 +	result = (*bqPlayerObject)->GetInterface( bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume );
   1.400 +	if ( SL_RESULT_SUCCESS != result ) {
   1.401 +
   1.402 +		LOGE( "SL_IID_VOLUME interface get failed" );
   1.403 +//		goto failed;
   1.404 +	}
   1.405 +
   1.406 +	// set the player's state to playing
   1.407 +	result = (*bqPlayerPlay)->SetPlayState( bqPlayerPlay, SL_PLAYSTATE_PLAYING );
   1.408 +	if ( SL_RESULT_SUCCESS != result ) {
   1.409 +
   1.410 +		LOGE( "Play set state failed" );
   1.411 +		goto failed;
   1.412 +	}
   1.413 +
   1.414 +    /* Create the audio buffer semaphore */
   1.415 +	playsem = SDL_CreateSemaphore( NUM_BUFFERS - 1 );
   1.416 +	if ( !playsem ) {
   1.417 +
   1.418 +		LOGE( "cannot create Semaphore!" );
   1.419 +		goto failed;
   1.420 +	}
   1.421 +
   1.422 +	/* Create the sound buffers */
   1.423 +	mixbuff = (Uint8 *) SDL_malloc( NUM_BUFFERS * this->spec.size );
   1.424 +	if ( mixbuff == NULL) {
   1.425 +
   1.426 +		LOGE( "mixbuffer allocate - out of memory" );
   1.427 +		goto failed;
   1.428 +	}
   1.429 +
   1.430 +	for ( i = 0; i < NUM_BUFFERS; i ++ )
   1.431 +		pmixbuff[i] = mixbuff + i * this->spec.size;
   1.432 +
   1.433 +    return 0;
   1.434 +
   1.435 +failed:;
   1.436 +
   1.437 +	openslES_DestroyPCMPlayer( );
   1.438 +
   1.439 +	return SDL_SetError( "Open device failed!" );
   1.440 +}
   1.441 +
   1.442 +static void openslES_DestroyPCMPlayer( void )
   1.443 +{
   1.444 +	// destroy buffer queue audio player object, and invalidate all associated interfaces
   1.445 +	if ( bqPlayerObject != NULL ) {
   1.446 +
   1.447 +        (*bqPlayerObject)->Destroy( bqPlayerObject );
   1.448 +
   1.449 +        bqPlayerObject		= NULL;
   1.450 +        bqPlayerPlay		= NULL;
   1.451 +        bqPlayerBufferQueue = NULL;
   1.452 +//        bqPlayerEffectSend	= NULL;
   1.453 +        bqPlayerMuteSolo	= NULL;
   1.454 +        bqPlayerVolume		= NULL;
   1.455 +    }
   1.456 +
   1.457 +	if ( playsem ) {
   1.458 +
   1.459 +		SDL_DestroySemaphore( playsem );
   1.460 +		playsem = NULL;
   1.461 +	}
   1.462 +
   1.463 +	if ( mixbuff )
   1.464 +		SDL_free( mixbuff );
   1.465 +
   1.466 +	return;
   1.467 +}
   1.468 +
   1.469 +static int openslES_OpenDevice( _THIS, void *handle, const char *devname, int iscapture )
   1.470 +{
   1.471 +	if ( iscapture ) {
   1.472 +		LOGI( "openslES_OpenDevice( ) %s for capture", devname );
   1.473 +		return openslES_CreatePCMRecorder( this );
   1.474 +	}
   1.475 +
   1.476 +	LOGI( "openslES_OpenDevice( ) %s for playing", devname );
   1.477 +
   1.478 +	return openslES_CreatePCMPlayer( this );
   1.479 +}
   1.480 +
   1.481 +static void openslES_CloseDevice( _THIS )
   1.482 +{
   1.483 +	if ( this->iscapture ) {
   1.484 +		LOGI( "openslES_CloseDevice( ) for capture" );
   1.485 +		return openslES_DestroyPCMRecorder( );
   1.486 +	}
   1.487 +
   1.488 +	LOGI( "openslES_CloseDevice( ) for playing" );
   1.489 +	openslES_DestroyPCMPlayer( );
   1.490 +
   1.491 +	return;
   1.492 +}
   1.493 +
   1.494 +static void openslES_WaitDevice( _THIS )
   1.495 +{
   1.496 +	LOGI( "openslES_WaitDevice( )" );
   1.497 +
   1.498 +    /* Wait for an audio chunk to finish */
   1.499 +//    WaitForSingleObject(this->hidden->audio_sem, INFINITE);
   1.500 +	SDL_SemWait( playsem );
   1.501 +
   1.502 +	return;
   1.503 +}
   1.504 +
   1.505 +///           n   playn sem
   1.506 +// getbuf     0   -     1
   1.507 +// fill buff  0   -     1
   1.508 +// play       0 - 0     1
   1.509 +// wait       1   0     0
   1.510 +// getbuf     1   0     0
   1.511 +// fill buff  1   0     0
   1.512 +// play       0   0     0
   1.513 +// wait       
   1.514 +//
   1.515 +// okay..
   1.516 +
   1.517 +
   1.518 +static Uint8 *openslES_GetDeviceBuf( _THIS )
   1.519 +{
   1.520 +	LOGI( "openslES_GetDeviceBuf( )" );
   1.521 +
   1.522 +    return pmixbuff[next_buffer];
   1.523 +}
   1.524 +
   1.525 +static void openslES_PlayDevice( _THIS )
   1.526 +{
   1.527 +	SLresult result;
   1.528 +
   1.529 +	LOGI( "======openslES_PlayDevice( )======" );
   1.530 +    /* Queue it up */
   1.531 +
   1.532 +	result = (*bqPlayerBufferQueue)->Enqueue( bqPlayerBufferQueue, pmixbuff[next_buffer], this->spec.size );
   1.533 +	if ( SL_RESULT_SUCCESS != result ) { 
   1.534 +		// just puk here
   1.535 +		// next !
   1.536 +	}
   1.537 +
   1.538 +	next_buffer ++;
   1.539 +	if ( next_buffer >= NUM_BUFFERS ) next_buffer = 0;
   1.540 +
   1.541 +	return;
   1.542 +}
   1.543 +
   1.544 +static int openslES_Init( SDL_AudioDriverImpl * impl )
   1.545 +{
   1.546 +	LOGI( "openslES_Init() called" );
   1.547 +
   1.548 +	if ( !openslES_CreateEngine() ) return 0;
   1.549 +
   1.550 +	LOGI( "openslES_Init() - set pointers" );
   1.551 +
   1.552 +	/* Set the function pointers */
   1.553 +//	impl->DetectDevices = openslES_DetectDevices;
   1.554 +	impl->OpenDevice    = openslES_OpenDevice;
   1.555 +   impl->PlayDevice	= openslES_PlayDevice;
   1.556 +   impl->GetDeviceBuf	= openslES_GetDeviceBuf;
   1.557 +	impl->Deinitialize  = openslES_DestroyEngine;
   1.558 +   impl->WaitDevice	= openslES_WaitDevice;
   1.559 +
   1.560 +	/* and the capabilities */
   1.561 +	impl->HasCaptureSupport = 0; /* TODO */
   1.562 +	impl->OnlyHasDefaultOutputDevice = 1;
   1.563 +//	impl->OnlyHasDefaultInputDevice  = 1;
   1.564 +
   1.565 +	LOGI( "openslES_Init() - succes" );
   1.566 +
   1.567 +	return 1;   /* this audio target is available. */
   1.568 +}
   1.569 +
   1.570 +AudioBootStrap openslES_bootstrap = {
   1.571 +	"openslES", "opensl ES audio driver", openslES_Init, 0
   1.572 +};
   1.573 +
   1.574 +#endif /* SDL_AUDIO_DRIVER_OPENSLES */
   1.575 +
   1.576 +/* vi: set ts=4 sw=4 expandtab: */