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