19cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi/*
29cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
39cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi *
49cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
59cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * you may not use this file except in compliance with the License.
69cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * You may obtain a copy of the License at
79cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi *
89cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
99cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi *
109cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
119cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
129cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * See the License for the specific language governing permissions and
149cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi * limitations under the License.
159cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi */
169cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
179cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <stdlib.h>
189cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <stdio.h>
199cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <string.h>
209cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <unistd.h>
219cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <sys/time.h>
229cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <fcntl.h>
239cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
24c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES.h>
2591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
26c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_Android.h>
2791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
289cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
299cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define MAX_NUMBER_INTERFACES 3
319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define TIME_S_BETWEEN_BB_ON_OFF 3
339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
359cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi/* Exits the application if an error is encountered */
369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
379cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
389cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
399cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
409cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
4158432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten        fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line);
42c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
449cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
499cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path  */
50b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kastenvoid TestBassBoostPathFromFD(SLObjectItf sl, const char* path, int16_t boostStrength, bool alwaysOn)
519cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
529cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLresult  result;
539cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLEngineItf EngineItf;
549cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Objects this application uses: one player and an ouput mix */
569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLObjectItf  player, outputMix;
579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Source of audio data to play */
599cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataSource            audioSource;
6091bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
619cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataLocator_AndroidFD locatorFd;
6291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
6391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    SLDataLocator_URI       locatorUri;
6491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
659cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataFormat_MIME       mime;
669cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
679cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Data sinks for the audio player */
689cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataSink               audioSink;
699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataLocator_OutputMix  locator_outputmix;
709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
7100667fcca51d62236b538e6857b7e6b923453569Jean-Michel Trivi    /* Interfaces for the audio player */
729cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLPlayItf              playItf;
739cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLPrefetchStatusItf    prefetchItf;
749cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLBassBoostItf         bbItf;
759cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLboolean required[MAX_NUMBER_INTERFACES];
779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        iidArray[i] = SL_IID_NULL;
879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
889cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configuration of the output mix  */
919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Create Output Mix object to be used by the player */
939cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     ExitOnError(result);
959cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
969cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realize the Output Mix object in synchronous mode */
979cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
989cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
999cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1009cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Setup the data sink structure */
1019cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
1029cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locator_outputmix.outputMix   = outputMix;
1039cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSink.pLocator            = (void*)&locator_outputmix;
1049cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSink.pFormat             = NULL;
1059cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1069cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
1079cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configuration of the player  */
1089cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
10900667fcca51d62236b538e6857b7e6b923453569Jean-Michel Trivi    /* Set arrays required[] and iidArray[] for required interfaces */
1109cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /*  (SLPlayItf is implicit) */
1119cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
1129cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    iidArray[0] = SL_IID_PREFETCHSTATUS;
1139cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
1149cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    iidArray[1] = SL_IID_BASSBOOST;
1159cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
11691bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
1179cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Setup the data source structure for the URI */
1189cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
1199cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    int fd = open(path, O_RDONLY);
1209cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (fd == -1) {
1219cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
1229cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1239cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.fd = (SLint32) fd;
1249cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
1259cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.offset = 0;
12691bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
12791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.locatorType = SL_DATALOCATOR_URI;
12891bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.URI = (SLchar *) path;
12991bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
1309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.formatType = SL_DATAFORMAT_MIME;
1329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
1339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
1349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.mimeType      = (SLchar*)NULL;
1359cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
1369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1379cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSource.pFormat  = (void*)&mime;
13891bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
1399cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSource.pLocator = (void*)&locatorFd;
14091bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
14191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    audioSource.pLocator = (void*)&locatorUri;
14291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
1439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1449cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Create the audio player */
1459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
1469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            iidArray, required);
1479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1499cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realize the player in synchronous mode. */
1509cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
1519cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "URI example: after Realize\n");
1529cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
15300667fcca51d62236b538e6857b7e6b923453569Jean-Michel Trivi    /* Get the SLPlayItf, SLPrefetchStatusItf and SLBassBoostItf interfaces for the player */
1549cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
1559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
1589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1599cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1609cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_BASSBOOST, (void*)&bbItf);
1619cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1629cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1639cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Player configured\n");
1649cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1659cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
1669cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Playback and test */
1679cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1689cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Start the data prefetching by setting the player to the paused state */
1699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
1709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1719cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1729cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Wait until there's data to play */
1739cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
1749cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
1759cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        usleep(100 * 1000);
1769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
1779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
1789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Get duration */
1819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
1829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->GetDuration(playItf, &durationInMsec);
1839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
1859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        durationInMsec = 5000;
1869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1889cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Start playback */
1899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Starting to play\n");
1909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
1919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1939cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configure BassBoost */
1949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
1959cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*bbItf)->IsStrengthSupported(bbItf, &strengthSupported);
1969cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1979cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (SL_BOOLEAN_FALSE == strengthSupported) {
1989cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "BassBoost strength is not supported on this platform. Too bad!\n");
1999cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    } else {
2009cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "BassBoost strength is supported, setting strength to %d\n", boostStrength);
2019cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        result = (*bbItf)->SetStrength(bbItf, boostStrength);
2029cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
2039cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2049cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2059cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLpermille strength = 0;
2069cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*bbItf)->GetRoundedStrength(bbItf, &strength);
2079cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2089cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Rounded strength of boost = %d\n", strength);
2099cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2109cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
211b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    /* Switch BassBoost on/off every TIME_S_BETWEEN_BB_ON_OFF seconds unless always on */
212b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    SLboolean previousEnabled = SL_BOOLEAN_FALSE;
213cb5ef4dc25d2f526898d12f008d551877347b9b8Jean-Michel Trivi    for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_BB_ON_OFF)) ; j++) {
214b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        SLboolean enabled;
2159cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        result = (*bbItf)->IsEnabled(bbItf, &enabled);
2169cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
217b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        enabled = alwaysOn || !enabled;
218b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        if (enabled != previousEnabled) {
219b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            result = (*bbItf)->SetEnabled(bbItf, enabled);
220b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            ExitOnError(result);
221b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            previousEnabled = enabled;
222b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            if (SL_BOOLEAN_TRUE == enabled) {
223b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten                fprintf(stdout, "BassBoost on\n");
224b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            } else {
225b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten                fprintf(stdout, "BassBoost off\n");
226b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten            }
2279cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        }
228b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        usleep(TIME_S_BETWEEN_BB_ON_OFF * 1000 * 1000);
2299cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Make sure player is stopped */
2329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Stopping playback\n");
2339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
2349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2359cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Destroy the player */
2379cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*player)->Destroy(player);
2389cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2399cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Destroy Output Mix object */
2409cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*outputMix)->Destroy(outputMix);
2419cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
24291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
2439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    close(fd);
24491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
2459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
2469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
2489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Triviint main(int argc, char* const argv[])
2499cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
250b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    const char *programName = argv[0];
2519cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLresult    result;
2529cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLObjectItf sl;
2539cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
254b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    fprintf(stdout, "OpenSL ES test %s: exercises SLBassBoostItf ", programName);
2559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
2569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Plays the sound file designated by the given path, ");
2579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "and applies a bass boost effect of the specified strength,\n");
2589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "where strength is a integer value between 0 and 1000.\n");
259b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    fprintf(stdout, "Every %d seconds, the BassBoost will be turned on and off,\n",
2609cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            TIME_S_BETWEEN_BB_ON_OFF);
261b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    fprintf(stdout, "unless the --always-on option is specified before the path.\n");
262b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten
263b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    bool alwaysOn = false;
264b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    if (argc >= 2 && !strcmp(argv[1], "--always-on")) {
265b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        alwaysOn = true;
266b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        --argc;
267b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        ++argv;
268b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    }
2699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (argc < 3) {
271b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        fprintf(stdout, "Usage: \t%s [--always-on] path bass_boost_strength\n", programName);
272b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", programName);
273c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
2749cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2759cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLEngineOption EngineOption[] = {
2779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
2789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    };
2799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
2819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
2849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
2859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    // intentionally not checking that argv[2], the bassboost strength, is between 0 and 1000
288b657e861b1a873a73cb9f492479c655dfdb68754Glenn Kasten    TestBassBoostPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]), alwaysOn);
2899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Shutdown OpenSL ES */
2919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*sl)->Destroy(sl);
2929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
293c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten    return EXIT_SUCCESS;
2949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
295