slesTestBassBoostPath.cpp revision 91bfd060b992810ca092f640f77f795bc3d008be
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
1791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
189cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define LOG_NDEBUG 0
199cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define LOG_TAG "slesTest_bassboost"
209cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
219cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <utils/Log.h>
2291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
2391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#define LOGV printf
2491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
2591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten
269cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <getopt.h>
279cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <stdlib.h>
289cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <stdio.h>
299cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <string.h>
309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <unistd.h>
319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <sys/time.h>
329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include <fcntl.h>
339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include "SLES/OpenSLES.h"
3591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#include "SLES/OpenSLES_Android.h"
3791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
389cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
399cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
409cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define MAX_NUMBER_INTERFACES 3
419cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define MAX_NUMBER_OUTPUT_DEVICES 6
429cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define TIME_S_BETWEEN_BB_ON_OFF 3
449cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi/* Exits the application if an error is encountered */
479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
499cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
509cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
519cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
52cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758Jean-Michel Trivi        fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
539cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        exit(1);
549cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
599cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
609cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path  */
619cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivivoid TestBassBoostPathFromFD( SLObjectItf sl, const char* path, int16_t boostStrength)
629cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
639cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLresult  result;
649cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLEngineItf EngineItf;
659cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
669cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Objects this application uses: one player and an ouput mix */
679cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLObjectItf  player, outputMix;
689cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Source of audio data to play */
709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataSource            audioSource;
7191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
729cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataLocator_AndroidFD locatorFd;
7391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
7491bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    SLDataLocator_URI       locatorUri;
7591bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataFormat_MIME       mime;
779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Data sinks for the audio player */
799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataSink               audioSink;
809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLDataLocator_OutputMix  locator_outputmix;
819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Play and PrefetchStatus interfaces for the audio player */
839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLPlayItf              playItf;
849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLPrefetchStatusItf    prefetchItf;
859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLBassBoostItf         bbItf;
869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLboolean required[MAX_NUMBER_INTERFACES];
889cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
939cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
959cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
969cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
979cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        iidArray[i] = SL_IID_NULL;
989cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
999cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1009cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
1019cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configuration of the output mix  */
1029cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1039cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Create Output Mix object to be used by the player */
1049cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
1059cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     ExitOnError(result);
1069cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1079cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realize the Output Mix object in synchronous mode */
1089cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
1099cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1109cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1119cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Setup the data sink structure */
1129cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
1139cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locator_outputmix.outputMix   = outputMix;
1149cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSink.pLocator            = (void*)&locator_outputmix;
1159cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSink.pFormat             = NULL;
1169cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1179cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
1189cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configuration of the player  */
1199cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1209cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
1219cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /*  (SLPlayItf is implicit) */
1229cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
1239cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    iidArray[0] = SL_IID_PREFETCHSTATUS;
1249cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
1259cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    iidArray[1] = SL_IID_BASSBOOST;
1269cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
12791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
1289cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Setup the data source structure for the URI */
1299cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
1309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    int fd = open(path, O_RDONLY);
1319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (fd == -1) {
1329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
1339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.fd = (SLint32) fd;
1359cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
1369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    locatorFd.offset = 0;
13791bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
13891bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.locatorType = SL_DATALOCATOR_URI;
13991bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    locatorUri.URI = (SLchar *) path;
14091bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
1419cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1429cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.formatType = SL_DATAFORMAT_MIME;
1439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
1449cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
1459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.mimeType      = (SLchar*)NULL;
1469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
1479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSource.pFormat  = (void*)&mime;
14991bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
1509cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    audioSource.pLocator = (void*)&locatorFd;
15191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#else
15291bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten    audioSource.pLocator = (void*)&locatorUri;
15391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
1549cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Create the audio player */
1569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
1579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            iidArray, required);
1589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1599cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1609cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realize the player in synchronous mode. */
1619cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
1629cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "URI example: after Realize\n");
1639cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1649cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
1659cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
1669cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1679cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1689cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
1699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1719cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_BASSBOOST, (void*)&bbItf);
1729cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1739cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1749cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Player configured\n");
1759cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* ------------------------------------------------------ */
1779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Playback and test */
1789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Start the data prefetching by setting the player to the paused state */
1809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
1819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Wait until there's data to play */
1849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
1859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
1869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        usleep(100 * 1000);
1879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
1889cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
1899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Get duration */
1929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
1939cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->GetDuration(playItf, &durationInMsec);
1949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
1959cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
1969cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        durationInMsec = 5000;
1979cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
1989cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
1999cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Start playback */
2009cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Starting to play\n");
2019cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
2029cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2039cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2049cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Configure BassBoost */
2059cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
2069cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*bbItf)->IsStrengthSupported(bbItf, &strengthSupported);
2079cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2089cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (SL_BOOLEAN_FALSE == strengthSupported) {
2099cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "BassBoost strength is not supported on this platform. Too bad!\n");
2109cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    } else {
2119cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "BassBoost strength is supported, setting strength to %d\n", boostStrength);
2129cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        result = (*bbItf)->SetStrength(bbItf, boostStrength);
2139cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
2149cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2159cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2169cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLpermille strength = 0;
2179cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*bbItf)->GetRoundedStrength(bbItf, &strength);
2189cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2199cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Rounded strength of boost = %d\n", strength);
2209cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2219cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2229cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Switch BassBoost on/off every TIME_S_BETWEEN_BB_ON_OFF seconds */
2239cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLboolean enabled = SL_BOOLEAN_TRUE;
2249cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*bbItf)->SetEnabled(bbItf, enabled);
2259cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2269cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    for(unsigned int j=0 ; j<(durationInMsec/1000*TIME_S_BETWEEN_BB_ON_OFF) ; j++) {
2279cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        usleep(TIME_S_BETWEEN_BB_ON_OFF * 1000 * 1000);
2289cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        result = (*bbItf)->IsEnabled(bbItf, &enabled);
2299cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
2309cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
2319cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        result = (*bbItf)->SetEnabled(bbItf, enabled);
2329cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        if (SL_BOOLEAN_TRUE == enabled) {
2339cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            fprintf(stdout, "BassBoost on\n");
2349cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        } else {
2359cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            fprintf(stdout, "BassBoost off\n");
2369cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        }
2379cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        ExitOnError(result);
2389cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2399cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2409cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Make sure player is stopped */
2419cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Stopping playback\n");
2429cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
2439cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2449cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2459cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Destroy the player */
2469cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*player)->Destroy(player);
2479cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2489cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Destroy Output Mix object */
2499cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*outputMix)->Destroy(outputMix);
2509cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
25191bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#ifdef ANDROID
2529cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    close(fd);
25391bfd060b992810ca092f640f77f795bc3d008beGlenn Kasten#endif
2549cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
2559cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2569cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi//-----------------------------------------------------------------
2579cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Triviint main(int argc, char* const argv[])
2589cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi{
2599cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    LOGV("Starting %s\n", argv[0]);
2609cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2619cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLresult    result;
2629cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLObjectItf sl;
2639cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2649cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "OpenSL ES test %s: exercises SLBassBoostItf ", argv[0]);
2659cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
2669cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Plays the sound file designated by the given path, ");
2679cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "and applies a bass boost effect of the specified strength,\n");
2689cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "where strength is a integer value between 0 and 1000.\n");
2699cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    fprintf(stdout, "Every %d seconds, the BassBoost will be turned on and off.\n",
2709cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            TIME_S_BETWEEN_BB_ON_OFF);
2719cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2729cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    if (argc < 3) {
2739cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "Usage: \t%s path bass_boost_strength\n", argv[0]);
2749cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", argv[0]);
2759cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi        exit(1);
2769cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    }
2779cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2789cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    SLEngineOption EngineOption[] = {
2799cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
2809cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    };
2819cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2829cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
2839cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2849cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2859cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
2869cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
2879cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    ExitOnError(result);
2889cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2899cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    // intentionally not checking that argv[2], the bassboost strength, is between 0 and 1000
2909cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    TestBassBoostPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]));
2919cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2929cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    /* Shutdown OpenSL ES */
2939cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    (*sl)->Destroy(sl);
2949cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    exit(0);
2959cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi
2969cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi    return 0;
2979cd3c9e7a2395647b92c32f33c06cf9fd546b22fJean-Michel Trivi}
298