slesTestEqFdPath.cpp revision 7e01bc6208fb5b4a2a0019d67bf74373f8ee9428
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_NDEBUG 0 18#define LOG_TAG "slesTest_seekFdPath" 19 20#include <utils/Log.h> 21#include <getopt.h> 22#include <stdlib.h> 23#include <stdio.h> 24#include <string.h> 25#include <unistd.h> 26#include <sys/time.h> 27#include <fcntl.h> 28 29#include "SLES/OpenSLES.h" 30#include "SLES/OpenSLES_Android.h" 31 32 33#define MAX_NUMBER_INTERFACES 3 34 35#define TIME_S_BETWEEN_EQ_ON_OFF 3 36 37static int testMode; 38//----------------------------------------------------------------- 39/* Exits the application if an error is encountered */ 40#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 41 42void ExitOnErrorFunc( SLresult result , int line) 43{ 44 if (SL_RESULT_SUCCESS != result) { 45 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 46 exit(1); 47 } 48} 49 50 51//----------------------------------------------------------------- 52 53/* Play an audio path by opening a file descriptor on that path */ 54void TestEQPathFromFD( SLObjectItf sl, const char* path, SLAint64 offset, SLAint64 size) 55{ 56 SLresult result; 57 SLEngineItf EngineItf; 58 59 /* Objects this application uses: one player and an ouput mix */ 60 SLObjectItf player, outputMix; 61 62 /* Source of audio data to play */ 63 SLDataSource audioSource; 64 SLDataLocator_AndroidFD locatorFd; 65 SLDataFormat_MIME mime; 66 67 /* Data sinks for the audio player */ 68 SLDataSink audioSink; 69 SLDataLocator_OutputMix locator_outputmix; 70 71 /* Play and PrefetchStatus interfaces for the audio player */ 72 SLPlayItf playItf; 73 SLPrefetchStatusItf prefetchItf; 74 SLEqualizerItf eqItf; 75 76 SLboolean required[MAX_NUMBER_INTERFACES]; 77 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 78 79 /* Get the SL Engine Interface which is implicit */ 80 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 81 ExitOnError(result); 82 83 /* Initialize arrays required[] and iidArray[] */ 84 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 85 required[i] = SL_BOOLEAN_FALSE; 86 iidArray[i] = SL_IID_NULL; 87 } 88 89 /* ------------------------------------------------------ */ 90 /* Configuration of the output mix */ 91 92 /* Create Output Mix object to be used by the player */ 93 result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required); 94 ExitOnError(result); 95 96 /* Realize the Output Mix object in synchronous mode */ 97 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE); 98 ExitOnError(result); 99 100 /* Setup the data sink structure */ 101 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 102 locator_outputmix.outputMix = outputMix; 103 audioSink.pLocator = (void*)&locator_outputmix; 104 audioSink.pFormat = NULL; 105 106 /* ------------------------------------------------------ */ 107 /* Configuration of the player */ 108 109 /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */ 110 /* (SLPlayItf is implicit) */ 111 required[0] = SL_BOOLEAN_TRUE; 112 iidArray[0] = SL_IID_PREFETCHSTATUS; 113 required[1] = SL_BOOLEAN_TRUE; 114 iidArray[1] = SL_IID_EQUALIZER; 115 116 /* Setup the data source structure for the URI */ 117 locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD; 118 int fd = open(path, O_RDONLY); 119 if (fd == -1) { 120 ExitOnError(SL_RESULT_RESOURCE_ERROR); 121 } 122 locatorFd.fd = (SLint32) fd; 123 locatorFd.length = size; 124 locatorFd.offset = offset; 125 126 mime.formatType = SL_DATAFORMAT_MIME; 127 /* this is how ignored mime information is specified, according to OpenSL ES spec 128 * in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */ 129 mime.mimeType = (SLchar*)NULL; 130 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 131 132 audioSource.pFormat = (void*)&mime; 133 audioSource.pLocator = (void*)&locatorFd; 134 135 /* Create the audio player */ 136 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2, 137 iidArray, required); 138 ExitOnError(result); 139 140 /* Realize the player in synchronous mode. */ 141 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result); 142 fprintf(stdout, "URI example: after Realize\n"); 143 144 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/ 145 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 146 ExitOnError(result); 147 148 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 149 ExitOnError(result); 150 151 result = (*player)->GetInterface(player, SL_IID_EQUALIZER, (void*)&eqItf); 152 ExitOnError(result); 153 154 fprintf(stdout, "Player configured\n"); 155 156 /* ------------------------------------------------------ */ 157 /* Playback and test */ 158 159 /* Start the data prefetching by setting the player to the paused state */ 160 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 161 ExitOnError(result); 162 163 /* Wait until there's data to play */ 164 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 165 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) { 166 usleep(100 * 1000); 167 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 168 ExitOnError(result); 169 } 170 171 /* Get duration */ 172 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 173 result = (*playItf)->GetDuration(playItf, &durationInMsec); 174 ExitOnError(result); 175 if (durationInMsec == SL_TIME_UNKNOWN) { 176 durationInMsec = 5000; 177 } 178 179 /* Start playback */ 180 fprintf(stdout, "Starting to play\n"); 181 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING ); 182 ExitOnError(result); 183 184 /* Configure EQ */ 185 SLuint16 nbPresets, preset, nbBands = 0; 186 result = (*eqItf)->GetNumberOfBands(eqItf, &nbBands); 187 ExitOnError(result); 188 result = (*eqItf)->GetNumberOfPresets(eqItf, &nbPresets); 189 ExitOnError(result); 190 /* Start from a preset */ 191 preset = nbPresets > 2 ? 2 : 0; 192 result = (*eqItf)->UsePreset(eqItf, preset); 193 194 preset = 1977; 195 result = (*eqItf)->GetCurrentPreset(eqItf, &preset); 196 ExitOnError(result); 197 if (SL_EQUALIZER_UNDEFINED == preset) { 198 fprintf(stderr, "Using SL_EQUALIZER_UNDEFINED preset, unexpected here!\n"); 199 } else { 200 fprintf(stdout, "Using preset %d\n", preset); 201 } 202 203 /* Tweak it so it's obvious it gets turned on/off later */ 204 SLmillibel minLevel, maxLevel = 0; 205 result = (*eqItf)->GetBandLevelRange(eqItf, &minLevel, &maxLevel); 206 ExitOnError(result); 207 fprintf(stdout, "Band level range = %dmB to %dmB\n", minLevel, maxLevel); 208 209 SLuint16 b = 0; 210 for(b = 0 ; b < nbBands/2 ; b++) { 211 result = (*eqItf)->SetBandLevel(eqItf, b, minLevel); 212 ExitOnError(result); 213 } 214 for(b = nbBands/2 ; b < nbBands ; b++) { 215 result = (*eqItf)->SetBandLevel(eqItf, b, maxLevel); 216 ExitOnError(result); 217 } 218 219 SLmillibel level = 0; 220 for(b = 0 ; b < nbBands ; b++) { 221 result = (*eqItf)->GetBandLevel(eqItf, b, &level); 222 ExitOnError(result); 223 fprintf(stdout, "Band %d level = %dmB\n", b, level); 224 } 225 226 /* Switch EQ on/off every TIME_S_BETWEEN_EQ_ON_OFF seconds */ 227 SLboolean enabled = SL_BOOLEAN_TRUE; 228 result = (*eqItf)->SetEnabled(eqItf, enabled); 229 ExitOnError(result); 230 for(unsigned int j=0 ; j<(durationInMsec/1000*TIME_S_BETWEEN_EQ_ON_OFF) ; j++) { 231 usleep(TIME_S_BETWEEN_EQ_ON_OFF * 1000 * 1000); 232 result = (*eqItf)->IsEnabled(eqItf, &enabled); 233 ExitOnError(result); 234 enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE; 235 result = (*eqItf)->SetEnabled(eqItf, enabled); 236 if (SL_BOOLEAN_TRUE == enabled) { 237 fprintf(stdout, "EQ on\n"); 238 } else { 239 fprintf(stdout, "EQ off\n"); 240 } 241 ExitOnError(result); 242 } 243 244 /* Make sure player is stopped */ 245 fprintf(stdout, "Stopping playback\n"); 246 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 247 ExitOnError(result); 248 249 /* Destroy the player */ 250 (*player)->Destroy(player); 251 252 /* Destroy Output Mix object */ 253 (*outputMix)->Destroy(outputMix); 254 255 close(fd); 256} 257 258//----------------------------------------------------------------- 259int main(int argc, char* const argv[]) 260{ 261 LOGV("Starting %s\n", argv[0]); 262 263 SLresult result; 264 SLObjectItf sl; 265 266 fprintf(stdout, "OpenSL ES test %s: exercises SLEqualizerItf ", argv[0]); 267 fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n"); 268 fprintf(stdout, "Plays the sound file designated by the given path, "); 269 fprintf(stdout, "starting at the specified offset, and using the specified length.\n"); 270 fprintf(stdout, "Omit the length of the file for it to be computed by the system.\n"); 271 fprintf(stdout, "Every %d seconds, the EQ will be turned on and off.\n", TIME_S_BETWEEN_EQ_ON_OFF); 272 273 if (argc < 3) { 274 fprintf(stdout, "Usage: \t%s path offsetInBytes [sizeInBytes]\n", argv[0]); 275 fprintf(stdout, "Example: \"%s /sdcard/my.mp3 0 344460\" \n", argv[0]); 276 exit(1); 277 } 278 279 SLEngineOption EngineOption[] = { 280 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 281 }; 282 283 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 284 ExitOnError(result); 285 286 /* Realizing the SL Engine in synchronous mode. */ 287 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 288 ExitOnError(result); 289 290 if (argc == 3) { 291 fprintf(stdout, "\nno file size given, using SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE\n\n"); 292 TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), 293 SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE); 294 } else { 295 TestEQPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), (SLAint64)atoi(argv[3])); 296 } 297 298 /* Shutdown OpenSL ES */ 299 (*sl)->Destroy(sl); 300 exit(0); 301 302 return 0; 303} 304